Sohail
Sohail

Reputation: 780

In C# while creating a file second time will throw an exception

I am creating a file using file stream, but before that i am applying if condition to see if the file exist or not. When i click on button and if supppose file is there it deletes the file. Its ok, and again if i press the button the file gets created. At first time it works well.

Now the file is created, again if I press the button and it should delete but it is trhowing an exception saying that*The process cannot access the file 'C:\Hello1' because it is being used by another process.*

Below is my code

     private void button2_Click(object sender, EventArgs e)
    {
        string fileName = @"C:\Hello1";
        if

            (File.Exists(fileName))
        {
            File.Delete(fileName);
            MessageBox.Show("File is deleted");
        }
        else
        {
            FileInfo createFile = new FileInfo(fileName);
            FileStream create = createFile.Create();
            MessageBox.Show("Created");

        }
    }

So why I am not able to delete second time, My text file is not open also but still it is showing the exception.

Upvotes: 3

Views: 420

Answers (4)

Savanetech
Savanetech

Reputation: 206

Here is an example I used to write a local log:

            StreamWriter log;

            string fpath = string.Format(@"{0}\{1}.txt",GetDirectory(),DateTime.Now.ToString("yyy-MM-dd"));
            if (!File.Exists(fpath))
            {
                log = new StreamWriter(fpath);
            }
            else
            {
                log = File.AppendText(fpath);
            }

            log.WriteLine(string.Format("{0} ==> {1}",DateTime.Now.ToString("MM/dd/yyy HH:mm:ss"), Message));

            log.Dispose();
            log = null;

Upvotes: 0

Tomek
Tomek

Reputation: 3279

The file stream is still opened when you're trying second time, try this:

private void button2_Click(object sender, EventArgs e)
{
    string fileName = @"C:\Hello1";
    if

        (File.Exists(fileName))
    {
        File.Delete(fileName);
        MessageBox.Show("File is deleted");
    }
    else
    {
        FileInfo createFile = new FileInfo(fileName);
        using(FileStream create = createFile.Create())
        {
            MessageBox.Show("Created");
        }
    }
}

Upvotes: 2

Sohail
Sohail

Reputation: 780

Oh yes i got the answer,

I need to use

     private void button2_Click(object sender, EventArgs e)
{
    string fileName = @"C:\Hello1";
    if

        (File.Exists(fileName))
    {
        File.Delete(fileName);
        MessageBox.Show("File is deleted");
    }
    else
    {
        FileInfo createFile = new FileInfo(fileName);
        FileStream create = createFile.Create();
        MessageBox.Show("Created");
        create.Close();

    }
}

We need to use create.Close();

Upvotes: 0

Dave Zych
Dave Zych

Reputation: 21897

You're never closing your stream that created the file. Put your FileStream in a using statement, which will automatically clean up the open file handle.

using(FileStream create = createFile.Create())
{
    //code here
}

Upvotes: 3

Related Questions