0
我需要创建一个带有标题的文本文件。创建一个带有标题的.txt文件
我目前使用StreamWriter
创建txt文件,如果它是不可用的,但我需要写在一次头,因为每次我调用这个方法我会写一个新的行成.txt文件
下面是我的代码:
string line = userID + "\t" + fullname + "\t" + module + "\t" +
datetime + "\t" + SQLStatement;
string fileExcel, filePath, fileName;
fileExcel = "Audit.txt";
filePath = HttpContext.Current.Server.MapPath("~");
fileName = filePath + "\\" + fileExcel;
System.IO.StreamWriter file = new System.IO.StreamWriter(fileName, true);
file.WriteLine(line);
file.Close();
你真的应该在[使用](http://msdn.microsoft.com/en-us/library/yh598w02(v为一次性包装起来= vs.80).aspx)块。 – James 2012-07-31 08:41:19
@詹姆斯你不必在调用['StreamWriter.Close()'](http://msdn.microsoft.com/en-us/library/system.io.streamwriter.close.aspx)时,它会处理底层流。但是好的。 – CodeCaster 2012-07-31 08:46:32
如果在调用'file.Close()'之前发生异常*会发生什么。 – James 2012-07-31 08:48:05