简体   繁体   中英

Filesystemwatcher events

I am having a problem with some filewatcher events. The filewatcher has 4 events. Created, Changed, Deleted, Renamed. Now my Created event works fine, but the rest doesn't although the application does run the events. I made it so when the created event is activated, the file in the 1st directory will automatically be copied to the 2nd directory. And so on with the rest of the events(File in 1st directory deleted, automatically delete in the 2nd directory). Like for example, this is my Deleted Event:

private void fileSystemWatcher1_Deleted(object sender, FileSystemEventArgs e)
{
    if (!pause)
    {
         filepath = Path.Combine(source, e.Name);
         name = Path.GetFileNameWithoutExtension(filepath);
         File.Delete (target + e.Name); // Delete file in directory:"target"
         query = "delete from " + tablemysql + " where name='" + name + "'";
         Mysql();
    }
}

So when I debug it, it does run through the File.Delete (target + e.Name); but it doesn't delete the file in the second directory. The delete query is working in mysql.

At the top I defined the variable target :

String target = ConfigurationManager.AppSettings[@"Directory2"];

and in the app config:

<add key="Directory2" value="C:\Users\Loko\Desktop\Loko\3"/>

Now I dont see any difference in executing everything in the created event and the deleted event

It has to delete a file in the 2nd directory(target) when the deleted event is called.

To compare some stuff, this is my Created event which works fine:

private void fileSystemWatcher1_Created(object sender, FileSystemEventArgs e)
{
    if (!pause)
    {
        filepath = Path.Combine(source, e.Name);
        name = Path.GetFileNameWithoutExtension(filepath);
        extension = Path.GetExtension(e.FullPath);
        size = e.Name.Length;

        readquery = "select * from " + tablemysql + " where name='" + name + "'";
        Record();
        query = "INSERT INTO " + tablemysql + 
            " (name,size,last_edit,extension) VALUES('" + 
            name + "','" + size + "',now(),'" + extension + "')";
        Mysql();

        if (Directory.Exists(e.FullPath))
        {
            copyfolder();
            Directory.CreateDirectory(target);
        }
        else
        {
            if (WaitForFileAvailable(e.FullPath, TimeSpan.FromSeconds(30)))
            {
                var file = Path.Combine(source, e.Name);
                var copy_file = Path.Combine(target, e.Name);
                var destination = Path.Combine(target, 
                    Path.ChangeExtension(source, Path.GetExtension(source)));
                if (File.Exists(file))// Check to see if the file exists. 
                {                     
                    // If it does delete the file in the target and 
                    // copy the one from the source to the target.
                    File.Delete(copy_file);
                }
                File.Copy(e.FullPath, copy_file);

            }
        }
    }
}

What is going wrong here?

You state that your target is defined as "C:\\Users\\Loko\\Desktop\\Loko\\3" . Assuming your filename is "myfile.txt" (for example), then the line:

File.Delete (target + e.Name);

will be attempting to delete "C:\\Users\\Loko\\Desktop\\Loko\\3myfile.txt" , which is missing a path separator.

You should instead use Path.Combine .

File.Delete(Path.Combine(target, e.Name))

Remarks

You may have been expecting an exception to be thrown on the File.Delete call if the file does not exist. However, t he documentation states:

If the file to be deleted does not exist, no exception is thrown.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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