error when delete a file

S

Starbuck

Gi

The code below is designed to rename a picture file,
The first part works, the new file is created, the second part (delete old
file) comes up with a file in use error.
Any thoughts please

If List1.Text = "" Then Exit Sub
Dim t As New InputDialog
Dim oldName As String = List1.Text
Dim newName As String = t.InputBox("Enter New Name", "Photo Rename", "")
If newName <> "" Then
'first check for extension, which will be jpg
newName = newName.ToUpper
Dim exten As String = cf.right(oldName, 4)
If exten.ToUpper <> ".JPG" Then oldName = oldName & ".JPG"
exten = cf.right(newName, 4)
If exten.ToUpper <> ".JPG" Then newName = newName & ".JPG"
Dim fi As New FileInfo(tPath & oldName)
fi.CopyTo(tPath & newName)
fi.Delete()
Reg_txt_Change()
End If


Thanks in advance
 
T

The Grim Reaper

Replace...
Dim fi As New FileInfo(tPath & oldName)
fi.CopyTo(tPath & newName)
fi.Delete()

with...

File.Copy(tPath & oldName, tPath & newName)
File.Delete(tPath & oldName)

or...

File.Move(tPath & oldName, tPath & newName)

________________________________________
The Grim Reaper
 
S

Starbuck

Sorry Grim
Same Error
Actual Message is -
"The process cannot access the file "c:\test\Pictures\KEV.JPG" because it is
being used by another process."

Regards
 
T

The Grim Reaper

So much for a quick answer!!

The only line I don't understand in your code is this one;

What is the cf??

Your file is obviously being locked by another process... is there anything
else in your code that opens/reads/checks the file at any stage??
All open file references and streams should be closed before attempting any
file level control.

HTH.
__________________________________________
The Grim Reaper
 
S

Starbuck

Hi Grim

cf is short for common functions this is a DLL of functions etc we use. The
right function is a .Net version of the olde VB6 right command.

The only other way we look at the file is when we populate a list box with
all jpg's from a given folder. The code is thus -


Private Sub Reg_txt_Change()
Try
List1.Items.Clear()
tPath = gc.PicturesPath
If tPath = "" Then tPath = "c:\gmwin\Pictures\"
Dim di As New IO.DirectoryInfo(tPath)
Dim diar1 As IO.FileInfo() = di.GetFiles("*.jpg")
Dim dra As IO.FileInfo
'list the names of all files in the specified directory
For Each dra In diar1
List1.Items.Add(dra)
Next
Catch ex As Exception
ErrorBox(ex.Message)
End Try
End Sub

The other routine is then called when a user highlights a item on the list
and tries to rename it.

Kev
 
T

The Grim Reaper

Hmm... doesn't look like anything in your software is locking the file...
I'm a bit at a loss at to what to suggest next.

For the record, the .NET equivalent of Right(myString, 4) is
myString.SubString(myString.Length - 4)

Cumbersome, I know. But in your case, there are better ways of using the
Framework to achieve your aims.
For example, I would replace your chunk of code with...

If Not oldName.ToUpper.EndsWith(".JPG") Then oldName &= ".JPG"
If Not newName.ToUpper.EndsWith(".JPG") Then newName &= ".JPG"

Obviously only saves you 2 lines of code and an exten variable, but you get
the idea - do that 500 times, and you'll save a lot of memory, resources and
typing!!
I'll keep thinking about the process thing...
________________________________________________
The Grim Reaper
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top