C#写入文本文件不成功
来源:百度知道 编辑:UC知道 时间:2024/06/07 06:04:11
try
{
StreamWriter sw = new StreamWriter(@"UserName.txt");
sw.WriteLine(myName);
sw.Close();
}
catch (Exception){
//不处理
}
finally
{
Console.WriteLine("Executing finally block");
}
代码是这样的,UserName.txt文件在当前文件夹下,但是运行没有反应,请高手指教!
1.
catch (Exception){
//这个里面加句话,下个断点,看看是不是发生异常了。
}
2.
实在没办法,加上下面这句。
sw.Flush();//看看行不?
3. myName应该不等于“”吧。
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.IO;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string myName = "God";
try
{
StreamWriter sw = new StreamWriter(@"UserName.txt");
sw.WriteLine(myName);
sw.Close();
}
catch (Exception)
{
//不处理
}
finally
{
Console.WriteLine("Executing finally block");
}
}
}
}
是不是写到NTSF的盘了