Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File acessed by another process?

Status
Not open for further replies.

OddGamer

Programmer
Jun 17, 2009
7
US
I'm trying to write a simple simple program. It should be trivial, but I'm coming across an error I don't quite get. The program is a picture sorter. Nothing special, it just shows all the pictures in a directory one at a time and then lets me choose where to send them. I could do the same thing manually by looking at the pictures and moving them with cut/paste, this is just (supposed to be) faster. The problem is I keep getting an error when I try to move the file with code, telling me the file is being used by another process (which as far as I know it's not).

Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim fileList As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
        Me.Show()
        My.Computer.FileSystem.CurrentDirectory = "D:\BP2\Pictures\"
        fileList = My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.CurrentDirectory, FileIO.SearchOption.SearchTopLevelOnly, "*.*")
        For Each foundFile As String In fileList
            lstFiles.Items.Add(foundFile)
        Next
        pbShowPic.Image = Image.FromFile(lstFiles.Items(0))
        PicIndex = 0
        oldfile = ""
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        oldfile = lstFiles.Items(0)
        newfile = "D:\Nature\Pictures\Bears" & Mid(lstFiles.Items(0), InStrRev(lstFiles.Items(0), "\"))
        lstFiles.Items.RemoveAt(0)
        pbShowPic.Image = Image.FromFile(lstFiles.Items(0))
        My.Computer.FileSystem.RenameFile(oldfile, newfile)
    End Sub

Any ideas what I'm doing wrong? (Being a large noob here, I know...)
 
I assume it is giving you this message during the move. The process of showing the picture is probably keeping it open. You probably need to close the picture prior to moving it. Sometimes there is a time delay when closing a file so you may need to pause a second before move.

Just a guess.

Simi
 
Can't be that. I tried a variant where I stored the names in global variables and it was on the next button press that the file was moved, which means the picture box couldn't be the one using it as it was on a new picture by the time it was moving the first one. I'm thinking it has something to do with the way I got the file listing, that somehow that's tying up the files. Not sure.
 
Try
Code:
 IO.File.Move(OldName, NewName)
instead of
Code:
My.Computer.FileSystem.RenameFile(oldfile, newfile)

Zameer Abdulla
 
Still no go. I've even got it back to trying the move after the next button is clicked. :( Is there another way, perhaps, to get the file listing? Or to clear the fileList variable in case that's doing it?
 
No go. :( Is there a way to find out *what* process is holding up the file, perhaps through the error message or something so I can at least find out what it's suggesting the problem is? Or perhaps I should wipe out the 'fileList' variable after I'm finished with it so that it can't be the culprit?
 
Further update... I just tried a freeware program (an unlocker at which, after the exception came in, says the file isn't locked up... now I'm really confused. As a side note, I can *copy* the file normally, but not move or delete it.
 
The problem is image.fromFile locks the image, load the image with a filestream to keep it from locking.

Instead of:
pbShowPic.Image = Image.FromFile(lstFiles.Items(0))

try

Dim fs As FileStream
fs = New FileStream(lstFiles.Items(0))
pbShowPic.Image = System.Drawing.Image.FromStream(fs)
 
Nope. -_- Man this is irritating. No changes at all. The entire code as it stands now, just in case I'm making a noob mistake:

Code:
Imports System.IO
Public Class Form1
    Private PicIndex As Integer
    Private newfile As String
    Private oldfile As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim fs As FileStream
        Dim fileList As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
        Me.Show()
        My.Computer.FileSystem.CurrentDirectory = "D:\BP2\Pictures\"
        fileList = My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.CurrentDirectory, FileIO.SearchOption.SearchTopLevelOnly, "*.*")
        For Each foundFile As String In fileList
            lstFiles.Items.Add(foundFile)
        Next
        fs = New FileStream(lstFiles.Items(0), FileMode.Open)
        pbShowPic.Image = System.Drawing.Image.FromStream(fs)
        PicIndex = 0
        oldfile = ""
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim fs As FileStream
        If oldfile <> "" Then IO.File.Move(oldfile, newfile)
        oldfile = lstFiles.Items(0)
        newfile = "D:\Nature\Pictures\Bears" & Mid(lstFiles.Items(0), InStrRev(lstFiles.Items(0), "\"))
        lstFiles.Items.RemoveAt(0)
        fs = New FileStream(lstFiles.Items(0), FileMode.Open)
        pbShowPic.Image = System.Drawing.Image.FromStream(fs)
    End Sub
End Class

Obviously it's when I press the button the second time that I get the error.
 

That's strange, image.fromFile would definitly cause this type of problem. I'm still not positive it's not fixed, the file might still be locked from image.fromFile. I'd try re-booting to make shure the file gets unlocked and try the code again.
 
Ah. Okies! Shut down VB, re-opened, and now it works. Thanks for all the help! That has it! ^_^ Yay!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top