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();
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();
相关文章推荐
- C#读写txt文件
- C#判断txt文件中某个字符串是否存在的函数
- c#读取txt文件
- C#生成txt文件
- 【转】C#如何读取txt文件中每一行每一个数据
- C# 导出导入TXT文件
- c#txt文件存到string数组中报错
- 2 C#串口或TCP远程采集数据 chart图表使用示例 保存数据到access数据库和每日.txt文件并实时显示各参数曲线
- C# 读取txt文件
- c#.NET 写txt文件小例子
- C#读写txt文件的两种方法介绍
- C# 读写Txt文件
- 用C#压缩单个txt文件的最简单方式是什么
- c#asp.net 读取 写入 txt 文件 asp.net操作文件
- C# 读取txt 文件 乱码 解决方法
- c# 常用文檔轉換txt文件
- 【C#】读取和写入本地txt文件
- C#:点的坐标输出至TXT文件
- C#读写txt文件
- C# 删除某一个目录中所有的txt文件