将数据附加到C#中的现有文件 [英] Append data to existing file in C#

查看:59
本文介绍了将数据附加到C#中的现有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下代码,将数据原样附加到现有数据中,但是我的代码覆盖了此代码

i have written the following code to append the data with existing data as it is but my code overwrite this

我应该对代码附加数据进行哪些更改.

what should i do the changes to code append data.

protected void Page_Load(object sender, EventArgs e)
{
    fname = Request.Form["Text1"];
    lname = Request.Form["Text2"];
    ph = Request.Form["Text3"];
    Empcode = Request.Form["Text4"];

    string filePath = @"E:Employee.txt";
    if (File.Exists(filePath))
    {
        //StreamWriter SW;
        //SW = File.CreateText(filePath);
        //SW.Write(text);
        //SW.Close();
        FileStream aFile = new FileStream(filePath, FileMode.Create, FileAccess.Write);
        StreamWriter sw = new StreamWriter(aFile);
        sw.WriteLine(Empcode);
        sw.WriteLine(fname);
        sw.WriteLine(lname);
        sw.WriteLine(ph);
        sw.WriteLine("**********************************************************************");

        sw.Close();
        aFile.Close();
    }
    else
    {
        //sw.Write(text);
        //sw.Flush();
        //sw.Close();
        //StreamWriter SW;
        //SW = File.AppendText(filePath);
        //SW.WriteLine(text);
        //SW.Close();

        FileStream aFile = new FileStream(filePath, FileMode.Append, FileAccess.Write);
        StreamWriter sw = new StreamWriter(aFile);

        sw.WriteLine(Empcode);
        sw.WriteLine(fname);
        sw.WriteLine(lname);
        sw.WriteLine(ph);
        sw.WriteLine("**********************************************************************");

        sw.Close();
        aFile.Close();
        //System.IO.File.WriteAllText(filePath, text);
    }
    Response.Write("Employee Add Successfully.........");
}

推荐答案

doc .Append说:

打开文件(如果存在)并查找到文件末尾,或创建一个新文件.此操作需要FileIOPermissionAccess.Append权限.FileMode.Append只能与FileAccess.Write结合使用.尝试在文件末尾之前寻找位置会引发IOException异常,并且任何读取尝试均会失败并引发NotSupportedException异常.

Opens the file if it exists and seeks to the end of the file, or creates a new file. This operation requires FileIOPermissionAccess.Append permission. FileMode.Append can be used only in conjunction with FileAccess.Write. Trying to seek to a position before the end of the file throws an IOException exception, and any attempt to read fails and throws an NotSupportedException exception.

因此不再需要 if 语句,因为 FileMode.Append 如果文件不存在,则会自动创建该文件.

So the if statement is no longer need because FileMode.Append automatically creates the file if it doesn't exist.

因此,完整的解决方案是:

The complete solution would therefore be:

using (FileStream aFile = new FileStream(filePath, FileMode.Append, FileAccess.Write))
using (StreamWriter sw = new StreamWriter(aFile)) {
    sw.WriteLine(Empcode);
    sw.WriteLine(fname);
    sw.WriteLine(lname);
    sw.WriteLine(ph);
    sw.WriteLine("**********************************************************************");
}

提示:使用 using 是因为它会在发生异常时自动关闭资源.

Hint: use using because it automatically closes the resources, also when an exception occurs.

这篇关于将数据附加到C#中的现有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆