我想打开一个文本文件,添加一行,然后关闭它。
当前回答
您可以使用File。它的AppendAllText:
File.AppendAllText(@"c:\path\file.txt", "text content" + Environment.NewLine);
其他回答
选择一个!但第一个非常简单。最后一个可能是用于文件操作的util:
//Method 1 (I like this)
File.AppendAllLines(
"FileAppendAllLines.txt",
new string[] { "line1", "line2", "line3" });
//Method 2
File.AppendAllText(
"FileAppendAllText.txt",
"line1" + Environment.NewLine +
"line2" + Environment.NewLine +
"line3" + Environment.NewLine);
//Method 3
using (StreamWriter stream = File.AppendText("FileAppendText.txt"))
{
stream.WriteLine("line1");
stream.WriteLine("line2");
stream.WriteLine("line3");
}
//Method 4
using (StreamWriter stream = new StreamWriter("StreamWriter.txt", true))
{
stream.WriteLine("line1");
stream.WriteLine("line2");
stream.WriteLine("line3");
}
//Method 5
using (StreamWriter stream = new FileInfo("FileInfo.txt").AppendText())
{
stream.WriteLine("line1");
stream.WriteLine("line2");
stream.WriteLine("line3");
}
技术上最好的方法可能是这样的:
private static async Task AppendLineToFileAsync([NotNull] string path, string line)
{
if (string.IsNullOrWhiteSpace(path))
throw new ArgumentOutOfRangeException(nameof(path), path, "Was null or whitepsace.");
if (!File.Exists(path))
throw new FileNotFoundException("File not found.", nameof(path));
using (var file = File.Open(path, FileMode.Append, FileAccess.Write))
using (var writer = new StreamWriter(file))
{
await writer.WriteLineAsync(line);
await writer.FlushAsync();
}
}
您可以使用File。它的AppendAllText:
File.AppendAllText(@"c:\path\file.txt", "text content" + Environment.NewLine);
我们可以用
public StreamWriter(string path, bool append);
在打开文件时
string path="C:\\MyFolder\\Notes.txt"
StreamWriter writer = new StreamWriter(path, true);
第一个参数是保存完整文件路径的字符串 第二个参数是Append Mode,在本例中为true
写入文件可以通过:
writer.Write(字符串)
or
writer.WriteLine(字符串)
示例代码
private void WriteAndAppend()
{
string Path = Application.StartupPath + "\\notes.txt";
FileInfo fi = new FileInfo(Path);
StreamWriter SW;
StreamReader SR;
if (fi.Exists)
{
SR = new StreamReader(Path);
string Line = "";
while (!SR.EndOfStream) // Till the last line
{
Line = SR.ReadLine();
}
SR.Close();
int x = 0;
if (Line.Trim().Length <= 0)
{
x = 0;
}
else
{
x = Convert.ToInt32(Line.Substring(0, Line.IndexOf('.')));
}
x++;
SW = new StreamWriter(Path, true);
SW.WriteLine("-----"+string.Format("{0:dd-MMM-yyyy hh:mm:ss tt}", DateTime.Now));
SW.WriteLine(x.ToString() + "." + textBox1.Text);
}
else
{
SW = new StreamWriter(Path);
SW.WriteLine("-----" + string.Format("{0:dd-MMM-yyyy hh:mm:ss tt}", DateTime.Now));
SW.WriteLine("1." + textBox1.Text);
}
SW.Flush();
SW.Close();
}
文件。AppendText会这样做:
using (StreamWriter w = File.AppendText("textFile.txt"))
{
w.WriteLine ("-------HURRAY----------");
w.Flush();
}
推荐文章
- 如何获取HttpContext。目前在ASP。网络核心?
- 给定文件系统路径,是否有一种更短的方法来提取没有扩展名的文件名?
- 方法在字典中添加新项或更新现有项
- 确定执行脚本的路径
- 使用Lambda/Linq对列表进行对象排序
- 为什么我不能在.NET中定义一个结构的默认构造函数?
- TypeLoadException表示“没有实现”,但它已经实现了
- 如何在ASP中返回当前操作。NET MVC视图?
- 存储Kinect 2.0版本的动作到BVH文件
- SortedList和SortedDictionary之间的区别是什么?
- jQuery用户界面对话框。NET按钮回发
- 如何用批处理脚本对目录中的每个文件做一些事情
- 合并。net中的两个数组
- 如何在Visual Studio中启用NuGet包恢复?
- 如何在实体框架6中调用存储过程(代码优先)?