新手100分求助:怎么把一个字符串写入到用户指定目录里的TXT文件中啊? string str="function showtop(){}"+"\nfunction showbot(){}";如果指定的TXT不存在则创建,如果存在则先删除再创建,创建好之后把字符串写入到TXT文件中去啊?我刚学C#,不知道怎么做,请给我完整的代码好吗?谢谢各位大大了! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 StreamWriter SW;string filenmae=Application.StartupPath.ToString()+@"\log.txt";SW=File.AppendText(filenmae);SW.WriteLine(System.DateTime.Now.ToString("yyyyMMdd HH:mm:ss")+" "+str);SW.Close(); string str="function showtop(){}"+"\nfunction showbot(){}";string path = @"D:\MyTest.txt";if (!File.Exists(path)) { // Create a file to write to. using (StreamWriter sw = File.CreateText(path)) { sw.Write(str); }}else{ File.Delete(path); using (StreamWriter sw =new StreamWriter(path)) { sw.Write(str); }}要确保有权限 C#如何读取excel并将数据写入到EXCEL中的? 如何调用存储过存填充到dataGridView1控件中呀,谢谢指教! 写xml文件遇到的问题 知道一个类的名字怎么判断它是什么类型? ado.net如何中让数据库回滚?谢谢! 为什么我写的c#的程序在别的机器上不能运行? 如何编辑 .csproj文件 C#中如何将定义和实现分离? 菜鸟问题.求教算法 各位有没有试过有XP后再装2003?会不会把XP干掉?我想试没敢装呢! DEV控件奇怪的问题 在使用vs2003时,我想让软件自动在运算符号前后加上空格,就像vb那样~该如何做啊?
string filenmae=Application.StartupPath.ToString()+@"\log.txt";
SW=File.AppendText(filenmae);
SW.WriteLine(System.DateTime.Now.ToString("yyyyMMdd HH:mm:ss")+" "+str);
SW.Close();
string path = @"D:\MyTest.txt";
if (!File.Exists(path))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
sw.Write(str);
}
}
else
{
File.Delete(path);
using (StreamWriter sw =new StreamWriter(path))
{
sw.Write(str);
}
}要确保有权限