Creating and Deleting a File Using Code

Posted on 2007-08-02
Last Modified: 2010-04-23
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.
Question by:kiranboi
    LVL 10

    Accepted 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
    LVL 18

    Expert Comment


    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Suggested Solutions

    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now