Hi experts! I'm having a problem using a WebClient. It seems that I keep getting the old "process cannot access the file because it is being used by another process" excpetion. Somehow, my own code must be causing this problem, because even after a total reboot it has the same prob (ie no other executable is tying up the file that is needed).
Here is my code. I'm lazy - i put down debug points in the catch() blocks. I don't output anything to the screen. But you can see where it always "catches" by my comment.
try
{
WebClient client = new WebClient();
FileStream fileStream = new FileStream(tempFile,
FileMode.Open,
FileAccess.Read,
FileShare.None);
client.DownloadFile(Connec
tString, tempFile);
if (!fileStream.CanRead)
{
string fileName = fileStream.Name;
fileStream.Close();
fileStream = new FileStream(tempFile, FileMode.Open, FileAccess.Read);
}
StreamReader streamReader = new StreamReader(fileStream);
fileStream.Close();
fileStream = new FileStream(tempFile, FileMode.Open, FileAccess.Read);
streamReader = new StreamReader(fileStream);
string[] discreteInfo = null;
while (streamReader.Peek() != -1)
{
string Info = streamReader.ReadLine();
discreteInfo = delimitedInfo.Split(new char[1] {','});
}
hashTable.Add(symbol, discreteInfo);
}
catch(System.IO.IOExceptio
n)
{
;
}
catch(System.Net.WebExcept
ion)
{
; // debug point always stops here...
}
finally
{
}
Here is the error message output:
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.Net.WebException: An exception occurred during a WebClient request. ---> System.IO.IOException: The process cannot access the file "C:\DOCUME~1\Owner\LOCALS~
1\Temp\tmp
4E.tmp" because it is being used by another process.
at System.IO.__Error.WinIOErr
or(Int32 errorCode, String str)
at System.IO.FileStream..ctor
(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor
(String path, FileMode mode, FileAccess access)
at System.Net.WebClient.Downl
oadFile(St
ring address, String fileName)
--- End of inner exception stack trace ---
Could this be solved with .dispose() or using()? I havent ever used those before.
Anyway, I'm stuck on this problem and could use some expert advice. Many thanks for your suggestions!
Start Free Trial