Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 166
  • Last Modified:

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.
1 Solution

' 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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now