5

I am trying create and write to a text file in a C# application using the following code

System.IO.Directory.CreateDirectory(Server.MapPath("~\\count"));

using (System.IO.FileStream fs = new System.IO.FileStream("~/count/count.txt", System.IO.FileMode.Create))
using (System.IO.StreamWriter sw = new System.IO.StreamWriter("~/count/count.txt"))
{
    sw.Write("101");
}

string _count = System.IO.File.ReadAllText("~/count/count.txt");
Application["NoOfVisitors"] = _count;

but I get an error:

The process cannot access the file 'path' because it is being used by another process.

What is my error?

2 Answers 2

11

You're trying to open the file twice; your first using statements creates a FileStream that is not used, but locks the file, so the second using fails.

Just delete your first using line, and it should all work fine.

However, I'd recommend replacing all of that with File.WriteAllText, then there would be no using in your code, it'd be much simpler.

var dir = Server.MapPath("~\\count");
var file = Path.Combine(dir, "count.txt");

Directory.CreateDirectory(dir);
File.WriteAllText(file, "101");

var _count = File.ReadAllText(file);
Application["NoOfVisitors"] = _count;
Sign up to request clarification or add additional context in comments.

4 Comments

i need to restart visual studio or system ?
Restarting you PC should definitely fix it (unless it's an earlier part of your app locking it), but otherwise it depends on exactly what has it locked.
using (System.IO.StreamWriter sw = new System.IO.StreamWriter("~/count/count.txt")) { sw.Write("101"); }
Oh, I misread your code. See edited answer, you're trying to open the file twice, you don't need your first using line.
0
private void ExportTextFile()
    {
        #region  This region Used For Private variable.
        string lblName = null;
        string lblACN = null;
        string lblIFSC = null;
        string lblBCode = null;
        string lblAdd1 = null;
        string lblAdd2 = null;
        string lblAdd3 = null;
        string lblMobileNo = null;
        string lblEmai = null;

        #endregion

        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "";
        Response.ContentType = "application/text";
        StringBuilder Rowbind = new StringBuilder();
        for (int i = 0; i < GrdAcc.Rows.Count; i++)
        {
            CheckBox Chk_Status = (CheckBox)GrdAcc.Rows[i].FindControl

("ChkStatus");
            Label lbl_Status = (Label)GrdAcc.Rows[i].FindControl

("lblStatus");
            Label lbl_Code = (Label)GrdAcc.Rows[i].FindControl

("lblEmpCode");
            if (Chk_Status.Checked == true)
            {
                #region Fetching ACC Details From Database.
                AccPL.Status = lbl_Status.Text;
               AccPL.EmpCode = lbl_Code.Text;
                DataTable dt = AccBL.ExportFileAcc(AccPL);
                if (dt.Rows.Count > 0)
                {
                    lblName = dt.Rows[0]["Name"].ToString().Trim();
                    lblACNo = dt.Rows[0]["ACNo"].ToString().Trim();
                    lblBCode = dt.Rows[0]["BCode"].ToString().Trim();
                    lblIFSC = dt.Rows[0]["IFSC"].ToString().Trim();
                    lblAdd1 = dt.Rows[0]["ADd1"].ToString() + dt.Rows[0]

["PPostTehsil"].ToString();
                    lblAdd2 = dt.Rows[0]["Add2"].ToString().Trim() + 

dt.Rows[0]["PPIN"].ToString().Trim();
                    lblAdd3 = dt.Rows[0]["Add3"].ToString() + dt.Rows[0]

["State"].ToString().Trim();
                    lblMobileNo = dt.Rows[0]["MobileNo"].ToString().Trim();
                    lblEmai = dt.Rows[0]["Email"].ToString().Trim();

                }
                #endregion

                #region Generating Text File .
                if (ddlExportType.SelectedValue == "2")
                {

                    Response.AddHeader("content-disposition", " 

attachment;filename=" + DateTime.Now.ToString("ddMMyyhhmmss") + ".txt");
                    Rowbind.Append(lblName + "#" + lblAccNo + "#" + lblIFSC 

+ "#" + "#" + "#" + "#" + "#" + "#" + lbl_Code.Text);


                    Rowbind.Append("\r\n");
                }
                #endregion

               
        Response.Output.Write(Rowbind.ToString());
        Response.Flush();
        Response.End();

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.