0

I have an ASP.NET Project, where I want to use the FileSystemWatcher. To do that I created an class "Watcher":

public class Watcher
    {

        [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
        public static void Run()
        {
            Console.WriteLine("FileWatcherStarted");
            using (FileSystemWatcher watcher = new FileSystemWatcher())
            {
                watcher.Path = @"C:\\FilesToWatch";

                watcher.NotifyFilter = NotifyFilters.LastAccess
                                     | NotifyFilters.LastWrite
                                     | NotifyFilters.FileName
                                     | NotifyFilters.DirectoryName;

                watcher.Filter = "*.txt";

                watcher.Changed += OnChanged;
                watcher.Created += OnChanged;
                watcher.Deleted += OnChanged;

                watcher.EnableRaisingEvents = true;
            }
        }

        private static void OnChanged(object source, FileSystemEventArgs e) =>
            Console.WriteLine($"{e.FullPath}, {e.ChangeType}");
    }

Then I call this Run method in the IActionResult Index() method in the home controller with:

Watcher.Run();

Now when I want to run or debug my project i get no result from my file watcher. No error occurs. Theres just the message "Thread 0x123 ended with Code 0 (0x0)". Anyone, who had the same problem or knows the solution?

Flo Hab
  • 20
  • 7
  • You are stopping and disposing it the second you create it – TheGeneral Jul 23 '19 at 08:13
  • 1
    look at your code. You create a watcher in `using` statement, so when this statement closes - your watcher is disposed. Creating `FileSystemWatcher` in controller is a bad idea anyway. – vasily.sib Jul 23 '19 at 08:15
  • What is the purpose of this? As mentioned above you should not be doing this in an ASP.Net application. If you can explain what you want to solve by doing this then maybe we can offer some better solutions. – Reinstate Monica Cellio Jul 23 '19 at 08:20
  • @vasily.sib I don't know, this is my first time working with this FileSystemWatcher :(, what would you change in the code in this case? – Flo Hab Jul 23 '19 at 08:20
  • @Archer when the ASP.NET Application is running and a file changed/created/deleted in this directory (path) then I want to get the full path of this new file. – Flo Hab Jul 23 '19 at 08:23
  • @FloHab The question explains that already, but I want to know *why* you want to do this. There will be a better way to do what you need. – Reinstate Monica Cellio Jul 23 '19 at 08:23
  • @Archer because in my project i read csv files and display them in an interactive chart. At the moment the user has to search for the csv file he want to display in the explorer. Now what I want is to offer an option to select the newest file in an given directory. – Flo Hab Jul 23 '19 at 08:27
  • 1
    In that case you just need to get the list of files when the user visits the page, and display them however you want. Are you expecting the page to just update when a new file is added to the folder? Bear in mind that a web application builds pages and then sleeps until the next request. It's not a long-running application like a Windows Forms app. – Reinstate Monica Cellio Jul 23 '19 at 08:36
  • @Archer yes it would also be great if I could display a list of the files. Then I select this file, parse it and save it into the db to get the data from there into the interactive chart. My principal said, that I get these files from server side and he also talked about the FileWatcher, but if there is any other way to do this I would be very happy about your tips :) – Flo Hab Jul 23 '19 at 08:42
  • The FileWatcher should really run in an executable (exe), not a web application, so it's not really suitable for this. What you should start with is getting a file list from the folder and displaying that so the user just clicks one and the details are displayed. Unless there is a requirement to take the file contents and store them in a database then I wouldn't bother, since the file contents are the data (you already have it). Updating the web page when new files arrive is a totally different game. I can advise you on it but you'd be better to focus on the first part first. – Reinstate Monica Cellio Jul 23 '19 at 08:48
  • 1
    @Archer ok, I will try. Thanks a lot for your time :) – Flo Hab Jul 23 '19 at 08:56

0 Answers0