IOException: The process cannot access the file 'filename' because it is being used by another process

456 views Asked by At

I have this code in my asp.net project:

protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            using (StreamWriter sw = File.CreateText(Server.MapPath(@"~/AboutUs.txt")))
            {
                sw.Write(FreeTextBox1.Text);
                sw.Close();
                sw.Dispose();
                lblError.Text = "تغییرات با موفقیت ذخیره شد.";
                lblError.CssClass = "success";
            }
        }
        catch (Exception ex)
        {
            lblError.Text = "خطایی روی داده است!" + "\n" + ex.ToString();
            lblError.CssClass = "error";

        }
        finally
        {
            lblError.Visible = true;
        }

    }

Sometimes (not always) when I hit btnSave following error is occurred:

IOException: The process cannot access the file 'filename' because it is being used by another process

Why?

1

There are 1 answers

0
karthi On

You just Run your application after see windows task manager-->Processes, any notepad.exe running you just right click then click end process . .