none
FileSystemWatcher To Watch Two Directories RRS feed

  • Question

  • I tried to write this, but I have failed :( - I am needing to watch a directory that may or may not exist.

    If the directory exists I want to convert any .docx inside to pdf

    How shold this code be changed so that it achieves that result?

    static FileSystemWatcher fsw = new FileSystemWatcher();

    static string[] locations = { @"R:\Customer Info\Customer1\Imports\Files\", @"X:\Info\Imports\Weekly\" }; static void Main(string[] args) { DateTime today = DateTime.Today; string s_today = today.ToString("MMddyyyy"); foreach (string path in locations) { string fullPath = Path.Combine(path + s_today + Path.DirectorySeparatorChar.ToString()); if (Directory.Exists(fullPath)) { fsw.Path = fullPath; fsw.Filter = "*.docx"; fsw.Created += OnCreated; fsw.EnableRaisingEvents = true; } } } private static void OnCreated(object source, FileSystemEventArgs e) { FileInfo file = new FileInfo(e.FullPath); Convert(file.ToString(), Path.GetDirectoryName(e.FullPath) + Path.DirectorySeparatorChar + "Test.pdf", WdSaveFormat.wdFormatPDF); Directory.EnumerateFiles(Path.GetDirectoryName(e.FullPath) + Path.DirectorySeparatorChar, "*.docx").ToList().ForEach(x => File.Delete(x)); }



    Friday, March 20, 2020 12:22 PM

All replies

  • Hello,

    The first thing to do is use Path.Combine for creating paths/file names rather than using + to concatenate. The second thing to do is set breakpoints and examine values and actions while in windows explorer mock up creating files. And make sure files you create match the file name pattern in your code.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, March 20, 2020 12:44 PM
    Moderator
  • Hi Karen

    My code works exactly as I need and describe as a Console app.  I'm wanting to use it as a windows service and (I've never done that before) so that is why i was thinking I need two filesystemwatchers

    Friday, March 20, 2020 12:51 PM
  • Hi IndigoMontoya,
    A single FileSystemWatcher can only monitor one directory and the files under this directory. Since you want to monitor multiple directories, so it is recommended to create multiple FileSystemWatcher instances to monitor these directories respectively. 
    There are some code examples in these threads you can refer to.
    [Filesystem watcher multiple directories]
    [How can I monitor multiple directories?]
    [Filesystem Watcher - Multiple folders]
    Hope these are helpful to you.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 24, 2020 7:36 AM