Note that there are some explanatory texts on larger screens.

plurals
  1. POEvents of watcher wont work as planned for synchronization program?
    primarykey
    data
    text
    <p>So I am having a synchronization program and I am using the filesystemwatcher to watch the changing in a directory. Now I had a listbox and I would add a different text at Changed, Created, Renamed and Deleted. Now what I noticed was, sometimes when I create a file it gives me the text: </p> <p>"File has been created"</p> <p>"File has been changed"</p> <p>I read somewhere that the filesystemwatcher shouldn't be used cause the events are triggered when sometimes it shouldn't be.</p> <p>Now I get this problem(I think it's because of the multiple events at once), When I drag a file in the directory I am watching, It should copy the file to the 2nd directory. If it already exists in the 2nd directory it should delete it and then copy it. Now I got the error:</p> <pre><code>The process cannot access the file because it is being used by another process </code></pre> <p>At the line where I delete the existing file in the 2nd directory. Now I was wondering wether the Filesystemwatcher is the problem here. Since it also triggers events and maybe it is still being "changed". which shouldn't have been triggered.</p> <p>My question is:</p> <p>Is the filesystemwatcher the problem?(So more like, is this a known problem?)</p> <p>If so:</p> <p>Is there something to replace the filewatcher with?</p> <p>If not, this is my code + explanations of some variables:</p> <p>Source = the first directory</p> <p>target= the second directory</p> <p>Record(); = executes readquery</p> <p>postgresql(); = inserts in postgressql with the strSelectCmd (Has nothing to do with it)</p> <pre><code> public static void copyfolder() { String source = ConfigurationManager.AppSettings[@"Directory1"]; String target = ConfigurationManager.AppSettings[@"Directory2"]; Copy(@source, @target); } </code></pre> <p>(string.IsNullOrEmpty(filename) == false) The actuall check if the file exists in the database.</p> <pre><code> private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e) { if (!pause) { logger("File created&gt; " + e.FullPath + " -Date:" + DateTime.Now); filepath = Path.Combine(source, e.Name); name = Path.GetFileNameWithoutExtension(filepath); extension = Path.GetExtension(e.FullPath); size = e.Name.Length; strSelectCmd = "INSERT INTO" + tablepostgresql + " (" + column1 + "," + column2 + "," + column3 + "," + column4 + ") VALUES('" + name + "','" + size + "',now(),'" + extension + "')"; readquery = "select * from " + tablemysql + " where name='" + name + "'"; postgresql(); Record(); if (string.IsNullOrEmpty(filename) == false) { 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(copy_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); } else // The file failed to become available within 10 seconds. { logger("Copy has failed reason: File is being used by another program"); } } } if (demo == "yes") { query = "INSERT INTO " + tablemysql + " (name,size,last_edit,extension) VALUES('" + name + "','" + size + "',now(),'" + extension + "')"; Mysql(); } } } </code></pre> <p>The error appears at this line:</p> <pre><code>File.Delete(copy_file); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload