Hi,
I've written a service to monitor a folder which is supposed to read a copied file (csv file) extract some information, close it and then move it to the another folder daily bases. file copy from another machine on the same domain. (service and Hot folder and moving destination are on same machine). so the problem is here:
1- whenever file be copy first time it's OK, it read and move it but second time which is next morning day while the service status is Start but it can't read file and show's error that
" System.IO.IOException:The process cannot access the file 'C:\HotFolder\123.csv'
System.IO.IOException: The process cannot access the file ' C:\HotFolder\123.csv' because it is being used by another process. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize) at System.IO.StreamReader..ctor(String path) at HotFoder.HotFolder.fileSystemWatcher1_Created(Object sender, FileSystemEventArgs e) in C:\HotFolderFolder\Service1.cs: line 44 "
which line 44 is "StreamReader sr = new StreamReader('C:\HotFolder\123.csv');"
at the same time if I drag and drop same file to Hot Folder it works like a charm.
so if anyone knows what is wrong with that please help me.
Thanks a lot
Loading
my develPosted May 30, 2012, 11:36 AM
try
{
// Open file here
System.Threading.Thread.Sleep(5000);
StreamReader sr = new treamReader('C:\HotFolder\123.csv');
...
... doing something
...
sr.Close();
sr.Dispose();
}
catch()
{
// Get thrown error
}
finally
{
}
my develPosted May 24, 2012, 11:01 AM
Thanks for your reply, my code is like this:
try
{
// Open file here
StreamReader sr = new StreamReader('C:\HotFolder\123.csv');
...
... doing something
...
sr.Close();
sr.Dispose();
}
catch()
{
// Get thrown error
}
finally
{
}
I've tried your code as well, same error, any other idea would be appreciated.
Thanks in advanced
Mahesh ChandPosted May 23, 2012, 4:15 PM
Here is some pseudo code:
File fl ;
try
{
// Open file here ..
// Do something
}
catch()
{
}
finally
{
// Close file here
fl.Close();
}
OR, you could do this:
using (File fl = File.Open() )
{
}
The second option is better b/c it takes care for you all closing and other stuff.