File Lock help in VB.net

  • Thread starter Thread starter Shane Saunders
  • Start date Start date
S

Shane Saunders

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
 
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/d...pref/html/frlrfSystemIOFileInfoClassTopic.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
 
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 said:
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.
 
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).TotalMilliseconds())
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
 
Hi,

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

Ken
---------------
 
* "Shane Saunders said:
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.
 
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
 
Back
Top