您的位置:首页 > 编程语言 > C#

c# 写txt文件

2013-06-07 14:49 106 查看
c# 写txt

public static void WriteErrorLogDataInteraction(string sLog, string titleLog)

{

try

{

string logPath = ConfigurationSettings.AppSettings["LogPath"]; //通过配置文件记录错误日志位置

if (string.IsNullOrEmpty(logPath))

{

logPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//配置文件安装目录

}

DateTime dt = DateTime.Now;

string logfile = new StringBuilder(logPath).Append(DateTime.Now.ToString("yyyy-MM")).Append(".txt").ToString();

if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(logfile)))

{

System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(logfile));

}

if (!File.Exists(logfile))

{

FileStream fs = System.IO.File.Create(logfile);

fs.Close();

}

using (StreamWriter sw = new StreamWriter(logfile, true))

{

sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":");

sw.WriteLine("Title: " + titleLog);

sw.WriteLine("Message:" + sLog);

sw.WriteLine();

sw.Close();

}

}

catch

{ }

}
方式二、
string a = rea.ToString();

StreamWriter bb = new StreamWriter("c:\\bbb.xml");

bb.Write(a);

bb.Close();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: