繁体   English   中英

如何使用Filesystemwatcher使用默认应用程序打开检测到的文件

[英]How to use Filesystemwatcher to Open Detected files with Default Application

如何修改Filesystemwatcher脚本以打开使用默认应用程序检测到的每个文件?

尝试使每个新检测到的文件(PDF)自动打开。 有针对这个的解决方法吗?

    static void Main(string[] args)
    {
        string path = @"C:\Users\Administrator\Documents\expo";

        FileSystemWatcher watcher = new FileSystemWatcher();


        watcher.Path = path;//assigning path to be watched
        watcher.EnableRaisingEvents = true;//make sure watcher will raise event in case of change in folder.
        watcher.IncludeSubdirectories = true;//make sure watcher will look into subfolders as well.
        watcher.Filter = "*.*"; //watcher should monitor all types of file.


        watcher.Created += watcher_Created; //register event to be called when a file is created in specified path
        watcher.Changed += watcher_Changed;//register event to be called when a file is updated in specified path
        watcher.Deleted += watcher_Deleted;//register event to be called when a file is deleted in specified path

        while (true) ;//run infinite loop so program doesn't terminate untill we force it.
    }

    static void watcher_Deleted(object sender, FileSystemEventArgs e)
    {
        Console.WriteLine("File : " + e.FullPath + " is deleted.");
    }

    static void watcher_Changed(object sender, FileSystemEventArgs e)
    {
        Console.WriteLine("File : " + e.FullPath + " is updated.");
    }

    static void watcher_Created(object sender, FileSystemEventArgs e)
    {
        Console.WriteLine("File : " + e.FullPath + " is created.");
    }

}

您可以使用以下代码使用默认应用程序打开文件:

static void watcher_Changed(object sender, FileSystemEventArgs e)
{
    Console.WriteLine("File : " + e.FullPath + " is updated.");

    // Open file with the default Application
    System.Diagnostics.Process.Start(e.FullPath); // Eg: "c:\myPDF.pdf"
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM