110,534
社区成员
发帖
与我相关
我的任务
分享
namespace FileStreamWrite
{
class Program
{
static void Main(string[] args)
{
FileStream fs = null;
string filePath = "C:\\file1.txt";
//将待写的入数据从字符串转换为字节数组
Encoding encoder = Encoding.UTF8;
byte[] bytes = encoder.GetBytes("Hello World! \n\r");
try
{
fs = File.OpenWrite(filePath);
//设定书写的开始位置为文件的末尾
fs.Position = fs.Length;
//将待写入内容追加到文件末尾
fs.Write(bytes, 0, bytes.Length);
}
catch (Exception ex)
{
Console.WriteLine("文件打开失败{0}", ex.ToString());
}
finally
{
fs.Close();
}
Console.ReadLine();
}
}
}
StringBuilder sb = new StringBuilder();
sb.AppendLine("测试");
File.WriteAllText(@"D:\test.txt",sb.ToString());
private void button2_Click(object sender, EventArgs e)
{
string path = @"F:\abc.txt";
System.IO.File.WriteAllText(path, textBox1.Text.Trim() + DateTime.Now.ToString() + "我的字符串" + path);
MessageBox.Show("保存成功!");
}