In my application, I am using a web client to download a file to the user's local machine. This works the first time, but after re-downloading I OCCASIONALLY recieve this run-time error:
*****************START***********************
An unhandled exception has occurred in a component in your application. Click continue and application will ignore this error and attempt to continue.
An exception occurred during a WebClient request.
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:\Weather Bar\current.wbd" because it is being used by another process.
at System.IO.__Error.WinIOError(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.DownloadFile(String address, String fileName)
--- End of inner exception stack trace ---
at System.Net.WebClient.DownloadFile(String address, String fileName)
at Weather_Bar.frmWeatherBar.GetCurrentWeather() in C:\CMPSC 115\Weather Bar\WeatherBar.vb:line 571
at Weather_Bar.frmWeatherBar.WeatherTimer_Tick(Object sender, EventArgs e) in C:\CMPSC 115\Weather Bar\WeatherBar.vb:line 679
at System.Windows.Forms.Timer.OnTick(EventArgs e)
at System.Windows.Forms.Timer.Callback(IntPtr hWnd, Int32 msg, IntPtr idEvent, IntPtr dwTime)
************** Loaded Assemblies **************
mscorlib
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///c:/windows/microsoft.net/framework/v1.1.4322/mscorlib.dll
----------------------------------------
Weather Bar
Assembly Version: 1.0.1439.34951
Win32 Version: 1.0.1439.34951
CodeBase: file:///C:/CMPSC%20115/Weather%20Bar/bin/Weather%20Bar.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///c:/windows/assembly/gac/system.windows.forms/1.0.5000.0__b77a5c561934e089/system.windows.forms.dll
----------------------------------------
System
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///c:/windows/assembly/gac/system/1.0.5000.0__b77a5c561934e089/system.dll
----------------------------------------
System.Drawing
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///c:/windows/assembly/gac/system.drawing/1.0.5000.0__b03f5f7f11d50a3a/system.drawing.dll
----------------------------------------
System.Xml
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///c:/windows/assembly/gac/system.xml/1.0.5000.0__b77a5c561934e089/system.xml.dll
----------------------------------------
Microsoft.VisualBasic
Assembly Version: 7.0.5000.0
Win32 Version: 7.10.3052.4
CodeBase: file:///c:/windows/assembly/gac/microsoft.visualbasic/7.0.5000.0__b03f5f7f11d50a3a/microsoft.visualbasic.dll
----------------------------------------
************** JIT Debugging **************
To enable just in time (JIT) debugging, the config file for this
application or machine (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.
For example:
<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>
When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the machine
rather than being handled by this dialog.
************************END*******************
The code I am using for this is below:
What is happening? It says it is being used by another process, but it is not. How can I prevent this error? Thanks in advance!
*****************START***********************
An unhandled exception has occurred in a component in your application. Click continue and application will ignore this error and attempt to continue.
An exception occurred during a WebClient request.
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:\Weather Bar\current.wbd" because it is being used by another process.
at System.IO.__Error.WinIOError(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.DownloadFile(String address, String fileName)
--- End of inner exception stack trace ---
at System.Net.WebClient.DownloadFile(String address, String fileName)
at Weather_Bar.frmWeatherBar.GetCurrentWeather() in C:\CMPSC 115\Weather Bar\WeatherBar.vb:line 571
at Weather_Bar.frmWeatherBar.WeatherTimer_Tick(Object sender, EventArgs e) in C:\CMPSC 115\Weather Bar\WeatherBar.vb:line 679
at System.Windows.Forms.Timer.OnTick(EventArgs e)
at System.Windows.Forms.Timer.Callback(IntPtr hWnd, Int32 msg, IntPtr idEvent, IntPtr dwTime)
************** Loaded Assemblies **************
mscorlib
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///c:/windows/microsoft.net/framework/v1.1.4322/mscorlib.dll
----------------------------------------
Weather Bar
Assembly Version: 1.0.1439.34951
Win32 Version: 1.0.1439.34951
CodeBase: file:///C:/CMPSC%20115/Weather%20Bar/bin/Weather%20Bar.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///c:/windows/assembly/gac/system.windows.forms/1.0.5000.0__b77a5c561934e089/system.windows.forms.dll
----------------------------------------
System
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///c:/windows/assembly/gac/system/1.0.5000.0__b77a5c561934e089/system.dll
----------------------------------------
System.Drawing
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///c:/windows/assembly/gac/system.drawing/1.0.5000.0__b03f5f7f11d50a3a/system.drawing.dll
----------------------------------------
System.Xml
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///c:/windows/assembly/gac/system.xml/1.0.5000.0__b77a5c561934e089/system.xml.dll
----------------------------------------
Microsoft.VisualBasic
Assembly Version: 7.0.5000.0
Win32 Version: 7.10.3052.4
CodeBase: file:///c:/windows/assembly/gac/microsoft.visualbasic/7.0.5000.0__b03f5f7f11d50a3a/microsoft.visualbasic.dll
----------------------------------------
************** JIT Debugging **************
To enable just in time (JIT) debugging, the config file for this
application or machine (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.
For example:
<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>
When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the machine
rather than being handled by this dialog.
************************END*******************
The code I am using for this is below:
Visual Basic:
Dim wc As New System.Net.WebClient 'Class level variable
wc.DownloadFile("http://www.filename.com/file.html", "c:\File\file.html")
What is happening? It says it is being used by another process, but it is not. How can I prevent this error? Thanks in advance!