将数据附加到 C# 中的现有文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10946421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Append data to existing file in C#
提问by swapnil
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.........");
}
采纳答案by Johannes Egger
The docfor FileMode.Append says:
FileMode.Append的文档说:
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.
打开文件(如果存在)并查找到文件末尾,或创建一个新文件。此操作需要 FileIOPermissionAccess.Append 权限。FileMode.Append 只能与 FileAccess.Write 结合使用。尝试寻找文件末尾之前的位置会引发 IOException 异常,任何读取尝试都会失败并引发 NotSupportedException 异常。
So the ifstatement is no longer need because FileMode.Appendautomatically creates the file if it doesn't exist.
因此if不再需要该语句,因为FileMode.Append如果文件不存在,则会自动创建该文件。
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("**********************************************************************");
}
Hint: use usingbecause it automatically closes the resources, also when an exception occurs.
提示:使用using是因为它会自动关闭资源,发生异常时也是如此。
回答by Rajesh Subramanian
You have to put
你必须把
if (!File.Exists(filePath))
instead of
代替
if (File.Exists(filePath))
回答by Blorgbeard is out
You are creating the file if it exists, and appending if it doesn't. That's the opposite of what you want.
如果文件存在,则创建文件,如果不存在则追加。这与你想要的相反。
Change it to:
将其更改为:
if (!File.Exists(filePath))
回答by ekholm
Change File.Exists(filePath)to !File.Exists(filePath)Or even better, always use Append mode. It will create the file if it does not exist
更改File.Exists(filePath)为!File.Exists(filePath)或者甚至更好,始终使用追加模式。如果文件不存在,它将创建文件
回答by Csaba Benko
It seems to me that the if and else statements must be in the opposite way, doesn't it?
在我看来,if 和 else 语句一定是相反的,不是吗?
Now you create the file, when it exists, and appends when it doesn't. (Append also creates the file if that is not exists yet.)
现在您创建文件,当它存在时,并在它不存在时追加。(如果该文件尚不存在,Append 也会创建该文件。)

