如何新建文件 rt通过代码在程序所在的路径下建立一个新建的txt文档,并在此文档里写入预定的内容。再加深一次,如何建立一个自定义扩展名的文档,比如a.abc,并在里写入预定的内容? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 FileStream stream = new FileStream("a.abc", FileMode.Create); StreamWriter writer = new StreamWriter(stream); writer.WriteLine("hello world"); writer.Close(); stream.Close(); 创建文件,文件名可以随意。 //path:文件路径;content:要写入的内容 public static void WriteFile(string path, string content) { try { using (StreamWriter sw = new StreamWriter(path, false, Encoding.UTF8)) { sw.Write(content); } } catch(Exception ex) { throw ex; } } using (FileStream fs = new FileStream("D:\\abc.txt", FileMode.Create, FileAccess.Write)) { StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default); sw.Write("你要写的内容。"); sw.Close(); } 路过,我赞成1楼的先建一个FileStream再用StreamWriter写入.因为我看到MSDN是这样写的哈哈,不过我很喜欢用File类里面的方法来写入,楼主也试试吧比如:System.IO.File.WriteAllText(string path,string ContentsResizedEventArgs,Encoding.UTF8); 我的VS2005奇怪的记忆状态 如何实现按F10就开始执行某个方法? 用socket获得远程主机的htm页面,存到本地后,很多字符不能显示 急!!!!!! DataGridView中如何实现和ComboBox一样的功能 在C#中如何追加数组的一个元素? 如何绘制 Region 的边线框? 如何用C#编写一个设置本地IP地址的程序!(在线等) 一个正则表达式的问题,给分100 c#怎样对excel进行页面设置 请问我想在本地连接远程数据库语句该怎样写? C#在执行的过程中能否调动一个批处理程序 请教Visual Studio 2010的两个小问题
StreamWriter writer = new StreamWriter(stream);
writer.WriteLine("hello world");
writer.Close();
stream.Close();
public static void WriteFile(string path, string content)
{
try
{
using (StreamWriter sw = new StreamWriter(path, false, Encoding.UTF8))
{
sw.Write(content);
}
}
catch(Exception ex)
{
throw ex;
}
}
using (FileStream fs = new FileStream("D:\\abc.txt", FileMode.Create, FileAccess.Write))
{
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);
sw.Write("你要写的内容。");
sw.Close();
}