【问题标题】:how can i return the directory reflected by onchanged() method in other class如何返回其他类中 onchanged() 方法反映的目录
【发布时间】:2019-01-30 11:38:58
【问题描述】:

我搜索了很多关于在 c# 中捕获剪切、复制事件的信息。最后我决定在 onchanged() 方法中放置一个 console.write() 以通过剪切、复制操作显示受影响目录的路径,最后我想捕获控制台输出......我定义了一个方法 (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;
    }

}

}

【问题讨论】:

    标签: c# event-handling filesystemwatcher


    【解决方案1】:

    如果你想让一个类中的某些东西对另一个类可用,一个非常干净的选择是引发一个事件。就我个人而言,我会重构你的代码,这样你就有了一个 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);
                }
            }
    

    【讨论】:

    • 感谢您的努力...我是 c# 新手...我还有一个问题...您能描述一下什么是>> EventHandler>> 这个.ChangedEvent(this, cleanpath);
    • EventHandler 是一个通用的委托类型,基本上是一种快速简便的方式来获取 T 类型的事件(在本例中为字符串);它避免了必须声明委托等。泛型和 EventHandler 示例有很多链接。这与声明 List 的想法相同 - 例如列表,列表。作为一个事件,它需要 2 个参数 - sender 和 e (就像您的 onchanged 事件一样)。所以 this.ChangedEvent(this, cleanPath) 表示在这个实例上调用 ChangedEvent(不需要 this.),将当前实例作为发送者,将 cleanPath 作为 'e' 参数)。
    猜你喜欢
    • 1970-01-01
    • 2014-12-14
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多