The process cannot access the file because it is being used by another process
Hello guys,
I have encounter this exception
Quote:
The process cannot access the file because it is being used by another process
This is a windows service application.im using filesystemwatcher control to monitor a folder and then my VBA app. drop a CSV file to that folder.it works for the first time you drop a file but if i try to drop another file it will throw an exception.
please any idea.what ive done wrong here.
this is my code
Code:
private void folderWatcher_Created(object sender, FileSystemEventArgs e)
{
try
{
//Lookup all .csv in the given directory.
string[] localPath = Directory.GetFiles(@"C:\EG2003\COSBM\", "*.csv");
//Get the filename
string filename = localPath[0].Substring(localPath[0].Length - 7, 7);
//FilePath where we drop the file in HO.
String HOPath = string.Concat(@"C:\DTR_Fire\", filename);
using (testwebserver.COSBMService service = new testwebserver.COSBMService())
{
//service.WriteFile(HOPath, null, string.Empty);
using (FileStream fs = new FileStream(localPath[0], FileMode.Open, FileAccess.Read,FileShare.ReadWrite))
{
if (fs.Length > 0)
{
byte[] Data;
long ByteToRead;
while (fs.Position < fs.Length)
{
if (fs.Length - fs.Position >= 102400)
{
ByteToRead = 102400;
}
else
{
ByteToRead = fs.Length - fs.Position;
}
Data = new byte[ByteToRead];
fs.Read(Data, 0, unchecked((int)ByteToRead));
service.WriteFile(HOPath, Data, string.Empty);
}
}
}
}
}
catch (Exception ex)
{
LogMessageToFile(ex.Message);
}
}
Re: The process cannot access the file because it is being used by another process
Try closing the filestream.
Re: The process cannot access the file because it is being used by another process
Quote:
Originally Posted by
pukisoft
Try closing the filestream.
i did but i get this error:
Quote:
Only assignment, call, increment, decrement, and new object expressions can be used as a statement
Re: The process cannot access the file because it is being used by another process
Here's the thing I've found about using the FSW looking for newly created files. The even fires off when a file is created... created, but not necessarily finished. huh? eh? wha? Consider this: any process that writes to a file first has to CREATE it before it can be written to. So that's what's happening... the file is being created, but still written to by the other process when you try to access it. this was a problem we ran into in a former life of mine... so what we did was each time the create event was fired, we'd get the file name, and put it into an array (at the time, we didn't have generics, otherwise we'd have used a Queue(Of T)) and start a timer. the timer had a 30-second delay. This usually gave the external process (we were receiving files via FTP) to complete the file transfer. If another file came in during that 30 seconds, the timer was stopped, reset, and restarted, allowing another 30 seconds. When the timer expired, we then took the array and loop through each file in it and process them.
-tg
Re: The process cannot access the file because it is being used by another process
Yeah, ran into that problem in a previous job before.
Check out this link: http://bloggingabout.net/blogs/jschr.../06/12886.aspx
Here's the solution I came up with in the past: vb dot net forums.com/vb-net-general-discussion/40290-help-file-watcher-class-visual-basic-net.html#post116289 (remove the spaces)
@Mods - Very classy auto renaming the links to the other forum to ***************s.com.
Re: The process cannot access the file because it is being used by another process
Matt - the link gets renamed because of some previous bad behavior... it was blacklisted for some reason (I don't remember off hand what the problem was... but it is what it is).
-tg