Creating and Deleting a File Using Code

Hi All,
I'm using the following code in my app to create a 'lock' file, copy an existing file, then delete the 'lock' file again:

 System.IO.File.Create(AccessDataPath & "Backup.lck")
            Dim myFilename As String = Now.Hour & "-" & Now.Minute & "-" & Now.Second & " Backup"
            System.IO.File.Copy(AccessDataPath & "Central.mdb", AccessDataPath & myFilename & ".ybs")
            System.IO.File.Delete(AccessDataPath & "Backup.lck")

It creates the file OK and copies the other file but when it tries to delete the created 'lock' file it says the file is currently in use. Can someone tell me how to get around this problem.
Who is Participating?

' Create the file.
        Dim fs As FileStream = File.Create(path, 1024)
        Dim info As Byte() = New UTF8Encoding(True).GetBytes("This is some text in the file.")

        ' Add some information to the file.
        fs.Write(info, 0, info.Length)
        fs.Close() <-- You need to close the file
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.