Windows Service in C#.Net 2.0, moving files from directory
-
Hi, I tried to develop a windows service in C#.Net 2.0 to move files from a directory to another directory but my problem is this, when the file is uploaded that directory, event firs up, even the file is not completely uploaded, and it gives me error, as file is still uploading, is there any way to solve this problem other than while loop to check that file is completed uploaded and is acceable now, please suggest.
Many Thanks, Adnan Rafiq muhammadadnanrafiq@gmail.com
-
Hi, I tried to develop a windows service in C#.Net 2.0 to move files from a directory to another directory but my problem is this, when the file is uploaded that directory, event firs up, even the file is not completely uploaded, and it gives me error, as file is still uploading, is there any way to solve this problem other than while loop to check that file is completed uploaded and is acceable now, please suggest.
Many Thanks, Adnan Rafiq muhammadadnanrafiq@gmail.com
Hi Adnan, Are you using a FileSystemWatcher to monitor the upload directory? If so, you have to make a do/while loop and try do access the file in a try/catch block inside the do/while loop. If you can access the file (it doesn't throw an exception of being current in use) you set a flag in order to exit from the loop. I think it should look something like this:
bool uploadCompleted = false; do { try { File.Open("yourFileName", FileMode.Open, FileAccess.Read).Close(); uploadCompleted = true; } catch { } } while (uploadCompleted != true)
I done it before and it worked. I couldn't find another approach so solve it (even made some posts in CP to find an answer). The problem is that the event is fired up even when the copy/move/creation is not complete, so you can't access the file yet. :/ Someone developed a customized code do solve this and wrote an article about this wrapper here in CP, but I thought it was too complicated and done the almost same thing I'm saying after all. Hope it helps you... CYA -
Hi Adnan, Are you using a FileSystemWatcher to monitor the upload directory? If so, you have to make a do/while loop and try do access the file in a try/catch block inside the do/while loop. If you can access the file (it doesn't throw an exception of being current in use) you set a flag in order to exit from the loop. I think it should look something like this:
bool uploadCompleted = false; do { try { File.Open("yourFileName", FileMode.Open, FileAccess.Read).Close(); uploadCompleted = true; } catch { } } while (uploadCompleted != true)
I done it before and it worked. I couldn't find another approach so solve it (even made some posts in CP to find an answer). The problem is that the event is fired up even when the copy/move/creation is not complete, so you can't access the file yet. :/ Someone developed a customized code do solve this and wrote an article about this wrapper here in CP, but I thought it was too complicated and done the almost same thing I'm saying after all. Hope it helps you... CYAHi, Thanks for replying and understanding problem Please tell me one thing, if i implement this solution of do while loop, and the uploading directory is getting files every second what will happen in that case, i think its performance will be stuck out, as other thread have to wait to finish out the while loop, or i mean wt will happened in that case. using filesystemwatcher the only .net component available to watch a directory.............. Please i hope you will understand what i m trying to say.
Many Thanks, Adnan Rafiq muhammadadnanrafiq@gmail.com
-
Hi, Thanks for replying and understanding problem Please tell me one thing, if i implement this solution of do while loop, and the uploading directory is getting files every second what will happen in that case, i think its performance will be stuck out, as other thread have to wait to finish out the while loop, or i mean wt will happened in that case. using filesystemwatcher the only .net component available to watch a directory.............. Please i hope you will understand what i m trying to say.
Many Thanks, Adnan Rafiq muhammadadnanrafiq@gmail.com