Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've been working on this file watcher that will basically run and when if detects an update from this directory "G:\excelsheet\autoupdates" it will copy the new/modified files, and paste a copy into this directory "C:\Importantdocs\pnls". For some reason there is a compile error with respect to the 13 line in the code (bolded). Can someone please help?
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Security.Permissions;

namespace FancyListener
{
    class FileWatcher
    {
        FileSystemWatcher Watcher;
        string Directory ="G:\GMM\Public\FinPro";
        [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
        public FileWatcher(string Directory)
        {
            Watcher = new FileSystemWatcher(Directory);
            Watcher.Path = Directory;
            Watcher.NotifyFilter = NotifyFilters.LastWrite;
            Watcher.Created += new FileSystemEventHandler(OnCreated);
            // Begin watching.
            Watcher.EnableRaisingEvents = true;
            wait();
            
        }
        private void OnChanged(object source, FileSystemEventArgs e)
        {
            // Specify what is done when a file is changed, created, or deleted.
            Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
            int filename_parameter = 1;
            copy(filename_parameter);
            
        }
        private void OnCreated(object source, FileSystemEventArgs e)
        {
            // Specify what is done when a file is changed, created, or deleted.
            Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
            int filename_parameter = 2;
            copy(filename_parameter);
                
        }
        private void wait()
        {
            Console.WriteLine("Waiting");
            while (true)
            {
            }
        }
        public void copy(int testval)
        {

            DateTime valueDate = DateTime.Today.Date;
            string dateString = valueDate.ToString("dd-MM-yyyy");
            string filename_var;
            if (testval == 1)
                 filename_var = "rate_file_changed_" + @dateString;
            else
                 filename_var = "new_rate_file_" + @dateString;
            string fileName = filename_var;
            string sourcePath = @"G:\GMM\Public\FinPro";
            string targetPath =  @"\\eitsvrtwprsvc03.ibg.adroot.bmogc.net\Test_DP$";
            fileName = System.IO.Directory.GetFiles(sourcePath);
            string fileName_start = System.IO.Path.GetFileName(fileName);
            string destFile = System.IO.Path.Combine(targetPath, fileName_start);
            System.IO.File.Copy(fileName_start, destFile, true);
        }
    }
}
Posted

You need to either escape the "\" like this:

"\\"

or

add an "@" at the front of the string. You have it done with several other string file paths on your page.
 
Share this answer
 
Comments
Zoltán Zörgő 8-May-13 16:46pm    
That's it. Simple and easy. +5
Richard C Bishop 8-May-13 16:49pm    
Thank you Zoltan(sorry, don't know how to give the "a" and accent)!
Sergey Alexandrovich Kryukov 8-May-13 17:36pm    
Free advice: you can get the accent by copy and paste. :-)
Note that we don't know you name at all, without accents or with :-)
—SA
Ian A Davidson 8-May-13 19:46pm    
Alt + 0225 (on the number pad) = á :)
Richard C Bishop 9-May-13 9:45am    
Thank you for that. Now I can address Zoltán properly.
In addition to the bug spotted in Solution 1, I think you should be aware of some FileSystemWatcher problem described in these CodeProject articles:
FileSystemWatcher - Pure Chaos (Part 1 of 2)[^],
FileSystemWatcher - Pure Chaos (Part 2 of 2)[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900