0

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?

4
  • Your current code will give "Unrecognized escape sequence" compilation error so please post your real code how you assign target. Commented Nov 18, 2013 at 9:37
  • @ShadowWizard My whole code would be way too big... Commented Nov 18, 2013 at 9:41
  • Just the real code where you assign the variable target. Guess it doesn't matter now that the actual problem was found and fixed. :) Commented Nov 18, 2013 at 9:43
  • @ShadowWizard See edit, but I already fixed it. Commented Nov 18, 2013 at 9:44

1 Answer 1

3

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, the documentation states:

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

Sign up to request clarification or add additional context in comments.

1 Comment

Could you also help me with my changed and renamed events in chat?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.