470,631 Members | 1,607 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,631 developers. It's quick & easy.

File Lock help in VB.net

I want to know how do i unlock a file from a Process. I have program that
downloads 5 image files. I want to use this class for mutliple downloads of
the same file name. The file are being add into a collection and being
animated. This part works like it should, but when i try to download new
image with the same name to overwrite them, it give me an IO File Error,
telling me that there is another process using that file.

I have set the collection and the image file variables to nothing, and it
still give me the some problem. i am also loading a different form to do
that animation in...
Shane
Nov 20 '05 #1
7 7423
Hi Shane,

What class are you using to save the files, the behaviour is sometimes
different. For some you have to first remove it using the by instance the
fileinfo class. (When it are important files the best thins is renaming them
to temp, write the new, remove the renamed.

http://msdn.microsoft.com/library/de...ClassTopic.asp

Other classes allow direct overwritting, so when the above link does not
help, show us than the part of the code where you write the file, because
there are even more possibilties in this..

I hope this helps?

Cor
I want to know how do i unlock a file from a Process. I have program that
downloads 5 image files. I want to use this class for mutliple downloads of the same file name. The file are being add into a collection and being
animated. This part works like it should, but when i try to download new
image with the same name to overwrite them, it give me an IO File Error,
telling me that there is another process using that file.

I have set the collection and the image file variables to nothing, and it
still give me the some problem. i am also loading a different form to do
that animation in...
Shane

Nov 20 '05 #2
HI,

Open the image like this to prevent the file from being locked

Dim fs As New System.IO.FileStream("C:\camera.bmp",
IO.FileMode.Open)
Dim bm As New Bitmap(fs)
fs.Close()

Ken
--------------------

"Shane Saunders" <as******@georgeandgabe.com> wrote in message
news:eK**************@TK2MSFTNGP12.phx.gbl:
I want to know how do i unlock a file from a Process. I have program that

downloads 5 image files. I want to use this class for mutliple downloads
of
the same file name. The file are being add into a collection and being
animated. This part works like it should, but when i try to download new

image with the same name to overwrite them, it give me an IO File Error,

telling me that there is another process using that file.

I have set the collection and the image file variables to nothing, and it

still give me the some problem. i am also loading a different form to do

that animation in...
Shane


--
Outgoing mail is certified Virus Free.
Checked by AVG Anti-Virus (http://www.grisoft.com).
Version: 7.0.230 / Virus Database: 263.3.0 - Release Date: 6/12/2004
Nov 20 '05 #3
* "Shane Saunders" <as******@georgeandgabe.com> scripsit:
I want to know how do i unlock a file from a Process. I have program that
downloads 5 image files. I want to use this class for mutliple downloads of
the same file name. The file are being add into a collection and being
animated. This part works like it should, but when i try to download new
image with the same name to overwrite them, it give me an IO File Error,
telling me that there is another process using that file.

I have set the collection and the image file variables to nothing, and it
still give me the some problem. i am also loading a different form to do
that animation in...


You will have to dispose the images by calling their 'Dispose' method.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #4
I tried what you suggested to, but i get the same error message.. here is
the code i have wrote for it. it the form and the DLL.

Public Class CWeatherMaps
Private m_strMapType As String

Private Const strhttp As String = http://image.weather.com
Const str300MileMap As String =
"/looper/archive/us_pdx_ultraradar_large_usen/"
Private Const str600MileMap As String =
"/looper/archive/us_pdx_closeradar_large_usen/"

'*******************************

'* Save path of the map files

'*******************************

Private strSave1 As String = Application.StartupPath & "\maps\1l.jpg"
Private strSave2 As String = Application.StartupPath & "\maps\2l.jpg"
Private strSave3 As String = Application.StartupPath & "\maps\3l.jpg"
Private strSave4 As String = Application.StartupPath & "\maps\4l.jpg"
Private strSave5 As String = Application.StartupPath & "\maps\5l.jpg"

Public Property WeatherMap() As String
Get

End Get
Set(ByVal Value As String)
Try
Select Case Value

Case "300mile"
m_strMapType = str300MileMap

If m_strMapType = Nothing Then Exit Try

Case "600mile"
m_strMapType = str600MileMap
End Select

Catch exc As Exception
'MessageBox.Show("Can not Save Files or Files are locked or File not Found",
"Save File Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Property

Finally
Call MapDownload()

End Try
End Set

End Property

Private Function MapDownload()

Dim milliseconds As Double
Dim date1970 As New Date(1970, 1, 1, 12, 0, 0)
Dim strUniqueTime As String

Dim strDownload As String
Dim wc As New System.Net.WebClient()

milliseconds = CLng(DateTime.Now.Subtract(date1970).TotalMillisec onds())
strUniqueTime = "?" & milliseconds.ToString

Try

strDownload = strhttp & m_strMapType & "1L.jpg" & strUniqueTime
wc.DownloadFile(strDownload, strSave1)

strDownload = strhttp & m_strMapType & "2L.jpg" & strUniqueTime
wc.DownloadFile(strDownload, strSave2)

strDownload = strhttp & m_strMapType & "3L.jpg" & strUniqueTime
wc.DownloadFile(strDownload, strSave3)

strDownload = strhttp & m_strMapType & "4L.jpg" & strUniqueTime
wc.DownloadFile(strDownload, strSave4)

strDownload = strhttp & m_strMapType & "5L.jpg" & strUniqueTime
wc.DownloadFile(strDownload, strSave5)

Catch exc As IO.IOException

End Try
End Function
End Class

Public Class frmWeatherMap

'Create a counting variable for looping purposes...

Dim iCounter As Integer

'*******************************************

'* Collection for the Downloaded Image Files

'*******************************************

Dim imgMapList As New Collection()
'***********************
'* Image files Variables
'***********************

Dim img1l As Image
Dim img2l As Image
Dim img3l As Image
Dim img4l As Image
Dim img5l As Image

'*******************************
'* Save path of the map files
'*******************************

Dim strSave1 As String = Application.StartupPath & "\maps\1l.jpg"
Dim strSave2 As String = Application.StartupPath & "\maps\2l.jpg"
Dim strSave3 As String = Application.StartupPath & "\maps\3l.jpg"
Dim strSave4 As String = Application.StartupPath & "\maps\4l.jpg"
Dim strSave5 As String = Application.StartupPath & "\maps\5l.jpg"

Private Sub frmWeatherMap_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'************************************************* **************
'* Calls the collection Load Function and enables tmrImageLooper
'************************************************* **************
Call LoadCollection()
tmrImageLooper.Enabled = True
End Sub

Private Sub tmrImageLooper_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles tmrImageLooper.Tick

Try
'***************************
'* Hides Default load image
'***************************

picAnimationLoading.Visible = False

'Add 1 to the counting variable...
iCounter = iCounter + 1

'Set the picture of imgAni to the image number
'iCounter(which are stored in the ImageList...

picImageMap.Image = imgMapList.Item(iCounter)

'If iCounter equals 8, set it back to 0 so that
'the animation continues.
'8 is the maximum amount of frames in this animation.
'Change this number to the number of frames to animate...
If iCounter = 5 Then iCounter = 0

Catch exc As Exception
tmrImageLooper.Enabled = False
MessageBox.Show("There are no Image Files Loaded in to the Collection at
this time.", "Animation Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Me.Close()
Exit Sub
End Try
End Sub

Private Sub mnuClose_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuClose.Click
Me.Close()
End Sub

Function LoadCollection() As Image

'************************************************* ***
'* Addes the Image files to the Collection(imgMaplist)
'************************************************* ***

Try

img1l = Image.FromFile(strSave1)
imgMapList.Add(img1l)

img2l = Image.FromFile(strSave2)
imgMapList.Add(img2l)

img3l = Image.FromFile(strSave3)
imgMapList.Add(img3l)

img4l = Image.FromFile(strSave4)
imgMapList.Add(img4l)

img5l = Image.FromFile(strSave5)
imgMapList.Add(img5l)

Catch exc2 As IO.IOException
MessageBox.Show(exc2.Message)
Me.Close()
Exit Function
End Try
End Function

End Class
Nov 20 '05 #5
Hi,

Image.fromfile locks the file use the filestream method I posted
before.

Ken
---------------

"Shane Saunders" <as******@georgeandgabe.com> wrote in message
news:ub**************@TK2MSFTNGP11.phx.gbl:
I tried what you suggested to, but i get the same error message.. here is

the code i have wrote for it. it the form and the DLL.

Public Class CWeatherMaps
Private m_strMapType As String

Private Const strhttp As String = HYPERLINK
"http://image.weather.com"http://image.weather.com
Const str300MileMap As String =
"/looper/archive/us_pdx_ultraradar_large_usen/"
Private Const str600MileMap As String =
"/looper/archive/us_pdx_closeradar_large_usen/"

'*******************************

'* Save path of the map files

'*******************************

Private strSave1 As String = Application.StartupPath & "\maps\1l.jpg"
Private strSave2 As String = Application.StartupPath & "\maps\2l.jpg"
Private strSave3 As String = Application.StartupPath & "\maps\3l.jpg"
Private strSave4 As String = Application.StartupPath & "\maps\4l.jpg"
Private strSave5 As String = Application.StartupPath & "\maps\5l.jpg"

Public Property WeatherMap() As String
Get

End Get
Set(ByVal Value As String)
Try
Select Case Value

Case "300mile"
m_strMapType = str300MileMap

If m_strMapType = Nothing Then Exit Try

Case "600mile"
m_strMapType = str600MileMap
End Select

Catch exc As Exception
'MessageBox.Show("Can not Save Files or Files are locked or File not
Found",
"Save File Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Property

Finally
Call MapDownload()

End Try
End Set

End Property

Private Function MapDownload()

Dim milliseconds As Double
Dim date1970 As New Date(1970, 1, 1, 12, 0, 0)
Dim strUniqueTime As String

Dim strDownload As String
Dim wc As New System.Net.WebClient()

milliseconds = CLng(DateTime.Now.Subtract(date1970).TotalMillisec onds())

strUniqueTime = "?" & milliseconds.ToString

Try

strDownload = strhttp & m_strMapType & "1L.jpg" & strUniqueTime
wc.DownloadFile(strDownload, strSave1)

strDownload = strhttp & m_strMapType & "2L.jpg" & strUniqueTime
wc.DownloadFile(strDownload, strSave2)

strDownload = strhttp & m_strMapType & "3L.jpg" & strUniqueTime
wc.DownloadFile(strDownload, strSave3)

strDownload = strhttp & m_strMapType & "4L.jpg" & strUniqueTime
wc.DownloadFile(strDownload, strSave4)

strDownload = strhttp & m_strMapType & "5L.jpg" & strUniqueTime
wc.DownloadFile(strDownload, strSave5)

Catch exc As IO.IOException

End Try
End Function
End Class

Public Class frmWeatherMap

'Create a counting variable for looping purposes...

Dim iCounter As Integer

'*******************************************

'* Collection for the Downloaded Image Files

'*******************************************

Dim imgMapList As New Collection()
'***********************
'* Image files Variables
'***********************

Dim img1l As Image
Dim img2l As Image
Dim img3l As Image
Dim img4l As Image
Dim img5l As Image

'*******************************
'* Save path of the map files
'*******************************

Dim strSave1 As String = Application.StartupPath & "\maps\1l.jpg"
Dim strSave2 As String = Application.StartupPath & "\maps\2l.jpg"
Dim strSave3 As String = Application.StartupPath & "\maps\3l.jpg"
Dim strSave4 As String = Application.StartupPath & "\maps\4l.jpg"
Dim strSave5 As String = Application.StartupPath & "\maps\5l.jpg"

Private Sub frmWeatherMap_Load(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles MyBase.Load

'************************************************* **************
'* Calls the collection Load Function and enables tmrImageLooper
'************************************************* **************
Call LoadCollection()
tmrImageLooper.Enabled = True
End Sub

Private Sub tmrImageLooper_Tick(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles tmrImageLooper.Tick

Try
'***************************
'* Hides Default load image
'***************************

picAnimationLoading.Visible = False

'Add 1 to the counting variable...
iCounter = iCounter + 1

'Set the picture of imgAni to the image number
'iCounter(which are stored in the ImageList...

picImageMap.Image = imgMapList.Item(iCounter)

'If iCounter equals 8, set it back to 0 so that
'the animation continues.
'8 is the maximum amount of frames in this animation.
'Change this number to the number of frames to animate...
If iCounter = 5 Then iCounter = 0

Catch exc As Exception
tmrImageLooper.Enabled = False
MessageBox.Show("There are no Image Files Loaded in to the Collection at

this time.", "Animation Error", MessageBoxButtons.OK,
MessageBoxIcon.Error)
Me.Close()
Exit Sub
End Try
End Sub

Private Sub mnuClose_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuClose.Click
Me.Close()
End Sub

Function LoadCollection() As Image

'************************************************* ***
'* Addes the Image files to the Collection(imgMaplist)
'************************************************* ***

Try

img1l = Image.FromFile(strSave1)
imgMapList.Add(img1l)

img2l = Image.FromFile(strSave2)
imgMapList.Add(img2l)

img3l = Image.FromFile(strSave3)
imgMapList.Add(img3l)

img4l = Image.FromFile(strSave4)
imgMapList.Add(img4l)

img5l = Image.FromFile(strSave5)
imgMapList.Add(img5l)

Catch exc2 As IO.IOException
MessageBox.Show(exc2.Message)
Me.Close()
Exit Function
End Try
End Function

End Class


--
Outgoing mail is certified Virus Free.
Checked by AVG Anti-Virus (http://www.grisoft.com).
Version: 7.0.230 / Virus Database: 263.3.0 - Release Date: 6/12/2004
Nov 20 '05 #6
* "Shane Saunders" <as******@georgeandgabe.com> scripsit:
Private strSave1 As String = Application.StartupPath & "\maps\1l.jpg"
Private strSave2 As String = Application.StartupPath & "\maps\2l.jpg"
Private strSave3 As String = Application.StartupPath & "\maps\3l.jpg"
Private strSave4 As String = Application.StartupPath & "\maps\4l.jpg"
Private strSave5 As String = Application.StartupPath & "\maps\5l.jpg"


I would use 'Path.Combine' to combine path and file names.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #7
Ken,

Thank you for your help. When first made modified the code i was use the
other varibles and it was locking up, but i change some stuff and it work
good now

Thank you again

Shane
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:OI**************@tk2msftngp13.phx.gbl...
Hi,

Image.fromfile locks the file use the filestream method I posted
before.

Ken
---------------

"Shane Saunders" <as******@georgeandgabe.com> wrote in message
news:ub**************@TK2MSFTNGP11.phx.gbl:
I tried what you suggested to, but i get the same error message.. here is
the code i have wrote for it. it the form and the DLL.

Public Class CWeatherMaps
Private m_strMapType As String

Private Const strhttp As String = HYPERLINK
"http://image.weather.com"http://image.weather.com
Const str300MileMap As String =
"/looper/archive/us_pdx_ultraradar_large_usen/"
Private Const str600MileMap As String =
"/looper/archive/us_pdx_closeradar_large_usen/"

'*******************************

'* Save path of the map files

'*******************************

Private strSave1 As String = Application.StartupPath & "\maps\1l.jpg"
Private strSave2 As String = Application.StartupPath & "\maps\2l.jpg"
Private strSave3 As String = Application.StartupPath & "\maps\3l.jpg"
Private strSave4 As String = Application.StartupPath & "\maps\4l.jpg"
Private strSave5 As String = Application.StartupPath & "\maps\5l.jpg"

Public Property WeatherMap() As String
Get

End Get
Set(ByVal Value As String)
Try
Select Case Value

Case "300mile"
m_strMapType = str300MileMap

If m_strMapType = Nothing Then Exit Try

Case "600mile"
m_strMapType = str600MileMap
End Select

Catch exc As Exception
'MessageBox.Show("Can not Save Files or Files are locked or File not
Found",
"Save File Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Property

Finally
Call MapDownload()

End Try
End Set

End Property

Private Function MapDownload()

Dim milliseconds As Double
Dim date1970 As New Date(1970, 1, 1, 12, 0, 0)
Dim strUniqueTime As String

Dim strDownload As String
Dim wc As New System.Net.WebClient()

milliseconds = CLng(DateTime.Now.Subtract(date1970).TotalMillisec onds())

strUniqueTime = "?" & milliseconds.ToString

Try

strDownload = strhttp & m_strMapType & "1L.jpg" & strUniqueTime
wc.DownloadFile(strDownload, strSave1)

strDownload = strhttp & m_strMapType & "2L.jpg" & strUniqueTime
wc.DownloadFile(strDownload, strSave2)

strDownload = strhttp & m_strMapType & "3L.jpg" & strUniqueTime
wc.DownloadFile(strDownload, strSave3)

strDownload = strhttp & m_strMapType & "4L.jpg" & strUniqueTime
wc.DownloadFile(strDownload, strSave4)

strDownload = strhttp & m_strMapType & "5L.jpg" & strUniqueTime
wc.DownloadFile(strDownload, strSave5)

Catch exc As IO.IOException

End Try
End Function
End Class

Public Class frmWeatherMap

'Create a counting variable for looping purposes...

Dim iCounter As Integer

'*******************************************

'* Collection for the Downloaded Image Files

'*******************************************

Dim imgMapList As New Collection()
'***********************
'* Image files Variables
'***********************

Dim img1l As Image
Dim img2l As Image
Dim img3l As Image
Dim img4l As Image
Dim img5l As Image

'*******************************
'* Save path of the map files
'*******************************

Dim strSave1 As String = Application.StartupPath & "\maps\1l.jpg"
Dim strSave2 As String = Application.StartupPath & "\maps\2l.jpg"
Dim strSave3 As String = Application.StartupPath & "\maps\3l.jpg"
Dim strSave4 As String = Application.StartupPath & "\maps\4l.jpg"
Dim strSave5 As String = Application.StartupPath & "\maps\5l.jpg"

Private Sub frmWeatherMap_Load(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles MyBase.Load

'************************************************* **************
'* Calls the collection Load Function and enables tmrImageLooper
'************************************************* **************
Call LoadCollection()
tmrImageLooper.Enabled = True
End Sub

Private Sub tmrImageLooper_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles tmrImageLooper.Tick

Try
'***************************
'* Hides Default load image
'***************************

picAnimationLoading.Visible = False

'Add 1 to the counting variable...
iCounter = iCounter + 1

'Set the picture of imgAni to the image number
'iCounter(which are stored in the ImageList...

picImageMap.Image = imgMapList.Item(iCounter)

'If iCounter equals 8, set it back to 0 so that
'the animation continues.
'8 is the maximum amount of frames in this animation.
'Change this number to the number of frames to animate...
If iCounter = 5 Then iCounter = 0

Catch exc As Exception
tmrImageLooper.Enabled = False
MessageBox.Show("There are no Image Files Loaded in to the Collection at

this time.", "Animation Error", MessageBoxButtons.OK,
MessageBoxIcon.Error)
Me.Close()
Exit Sub
End Try
End Sub

Private Sub mnuClose_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnuClose.Click
Me.Close()
End Sub

Function LoadCollection() As Image

'************************************************* ***
'* Addes the Image files to the Collection(imgMaplist)
'************************************************* ***

Try

img1l = Image.FromFile(strSave1)
imgMapList.Add(img1l)

img2l = Image.FromFile(strSave2)
imgMapList.Add(img2l)

img3l = Image.FromFile(strSave3)
imgMapList.Add(img3l)

img4l = Image.FromFile(strSave4)
imgMapList.Add(img4l)

img5l = Image.FromFile(strSave5)
imgMapList.Add(img5l)

Catch exc2 As IO.IOException
MessageBox.Show(exc2.Message)
Me.Close()
Exit Function
End Try
End Function

End Class


--
Outgoing mail is certified Virus Free.
Checked by AVG Anti-Virus (http://www.grisoft.com).
Version: 7.0.230 / Virus Database: 263.3.0 - Release Date: 6/12/2004

Nov 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Pekka Niiranen | last post: by
4 posts views Thread by KDB | last post: by
6 posts views Thread by martin | last post: by
14 posts views Thread by Gary Nelson | last post: by
4 posts views Thread by muttu2244 | last post: by
5 posts views Thread by pgdown | last post: by
2 posts views Thread by WingSiu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.