我搜索了很多有关在c#中捕获剪切,复制事件的信息。最后,我决定将console.write()放在onchanged()方法中,以通过剪切,复制操作显示受影响目录的路径,最后我想把控制台拖出...我定义了一个方法(ochangepathgetter)返回(cleanpath,受影响目录的目录)。我的问题是我无法在其他课程中捕获此变量?任何人都可以修改此代码,以便我捕获此变量吗?
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Data.SqlClient;
namespace Consoleapplication1
{
class program
{
public int oc = 0;
static void Main()
{
FileStream ostrm;
StreamWriter writer;
TextWriter oldOut = Console.Out;
ostrm = new FileStream(@"D:\classic1\2.txt", FileMode.OpenOrCreate, FileAccess.Write);
writer = new StreamWriter(ostrm);
Console.SetOut(writer);
watcher();
Console.SetOut(oldOut);
writer.Close();
ostrm.Close();
Console.Read();
}
public static void watcher()
{
DriveInfo[] alldrives = DriveInfo.GetDrives();
foreach (DriveInfo drive in alldrives)
{
if (drive.Name.Contains('G'))
//drive.name != delimon.win32.io.path.getpathroot(environment.Systemdirectory)
{
DirectoryInfo[] alldir = new DirectoryInfo(drive.Name).GetDirectories("*.*", SearchOption.AllDirectories);
DirectoryInfo rootdir = new DirectoryInfo(drive.RootDirectory.Name);
foreach (DirectoryInfo dir in alldir)
{
FileSystemWatcher filewatcher = new FileSystemWatcher();
filewatcher.Path = dir.FullName;
filewatcher.Filter = "*.*";
filewatcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Size;
filewatcher.Created += new FileSystemEventHandler(onchanged);
filewatcher.Deleted += new FileSystemEventHandler(onchanged);
filewatcher.Renamed += new RenamedEventHandler(onrenamed);
{ filewatcher.EnableRaisingEvents = true; }
}
for (int i = 0; i < 1; i++)
{
FileSystemWatcher filewatcher = new FileSystemWatcher();
filewatcher.Path = rootdir.FullName;
filewatcher.Filter = "*.*";
filewatcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Size;
filewatcher.Created += new FileSystemEventHandler(onchanged);
filewatcher.Deleted += new FileSystemEventHandler(onchanged);
filewatcher.Renamed += new RenamedEventHandler(onrenamed);
{ filewatcher.EnableRaisingEvents = true; }
}
}
}
}
private static void onrenamed(object sender, RenamedEventArgs e)
{
Console.Write("the new name is " + e.Name + "and the old name was" + e.OldName);
}
private static void onchanged(object sender, FileSystemEventArgs e)
{
// Console.Write(e.FullPath);
int index1 = e.FullPath.IndexOf(e.Name);
string cleanpath = e.FullPath.Remove(index1, (e.FullPath.Length) - (index1));
Console.Write(cleanpath);
Console.Write(Environment.NewLine);
ochangepathgetter(cleanpath);
}
public static long foldersize(string din)
{
//string dn = din.FullName.ToString();
long leng = Directory.GetFiles(din, "*", SearchOption.AllDirectories).Sum(t => (new FileInfo(t).Length));
return leng;
}
public static string ochangepathgetter(string path)
{
return path;
}
}
}
答案 0 :(得分:0)
如果您想让某个班级中的某个项目对另一个人可用,那么一个很干净的选择就是引发一个事件。我个人将重构您的代码,以便您有一个Watcher类,该类引发cleanPath事件。例如。 (仅显示最相关的部分)
class program
{
static void Main()
{
var watcher = new Watcher();
watcher.Start();
// hook into the event
watcher.ChangedEvent += WatcherChanged;
Console.Read();
}
private static void WatcherChanged(object sender, string e)
{
Console.WriteLine($"clean path is {e}");
}
}
class Watcher
{
public EventHandler<string> ChangedEvent;
public void Start()
{
DriveInfo[] alldrives = DriveInfo.GetDrives();
// etc.
}
private void onchanged(object sender, FileSystemEventArgs e)
{
int index1 = e.FullPath.IndexOf(e.Name);
string cleanpath = e.FullPath.Remove(index1, (e.FullPath.Length) - (index1));
// fire the event
if (this.ChangedEvent != null)
{
this.ChangedEvent(this, cleanpath);
}
}