Как использовать Filesystemwatcher для открытия обнаруженных файлов с приложением по умолчанию - PullRequest
0 голосов
/ 23 сентября 2018

Как мне изменить мой скрипт Filesystemwatcher для открытия каждого файла, который он обнаруживает с помощью приложения по умолчанию?

Попытка сделать так, чтобы каждый вновь обнаруженный файл (PDFs) открывался автоматически.Есть ли решение для этого?

    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.");
    }

}

1 Ответ

0 голосов
/ 23 сентября 2018

Вы можете открыть файл с приложением по умолчанию с кодом ниже:

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"
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...