繁体   English   中英

如何创建 .txt 文件并编写它 c# asp.net

[英]how to create .txt file and write it c# asp.net

我正在尝试使用以下代码在 C# 应用程序中创建和写入文本文件

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;

但我收到一个错误:

该进程无法访问文件“路径”,因为它正被另一个进程使用。

我的错误是什么?

你试图打开文件两次; 您的第一个using语句创建了一个未使用的FileStream ,但会锁定文件,因此第二个using失败。

只需删除您的第一个using行,它应该一切正常。

但是,我建议将所有这些替换为File.WriteAllText ,然后在您的代码中就没有使用了,它会简单得多。

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;
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();

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM