今天做了日志文件读写,使用方法FileStream ,StreamWriter ,File
2010-10-27 11:58
471 查看
//异常处理
public static bool GeException(string strMessage, string strName)
{
string time = System.DateTime.Now.ToString();
string strPathName = System.DateTime.Now.ToShortDateString() + "_" + strName + "_Error__Log.txt";
//指定路径
string strPath = "C:\\EWJAR_LoG";
if (!Directory.Exists(strPath))
{
DirectoryInfo di = Directory.CreateDirectory(strPath);
}
strPathName = strPath + "\\" + strPathName;
WirteFile(strPathName, strMessage);
////如果文件a.txt存在就打开,不存在就新建 .append 是追加写
//FileStream fst = new FileStream(strPathName, FileMode.Append);
////写数据到a.txt格式
//StreamWriter swt = new StreamWriter(fst, System.Text.Encoding.GetEncoding("utf-8"));
////写入
//swt.WriteLine(time + '\r' + "错误信息开始:" + strMessage);
//swt.Close();
//fst.Close();
return true;
}
/// <summary>
/// 判断文件否存在,不存在,创建,存在追加重写
/// </summary>
/// <param name="strPath">文绝对路径</param>
/// <param name="strMessage">消息</param>
public static void WirteFile(string strPath, string strMessage)
{
string time = System.DateTime.Now.ToString();
//判断文件的存在
if (File.Exists(strPath))
{
//存文件
//如果文件a.txt存在就打开,不存在就新建 .append 是追加写
FileStream fst = new FileStream(strPath, FileMode.Append);
//写数据到a.txt格式
StreamWriter swt = new StreamWriter(fst, System.Text.Encoding.GetEncoding("utf-8"));
//写入
swt.WriteLine(time + strMessage);
swt.Close();
fst.Close();
}
else
{
//不存在文件
FileStream fst = new FileStream(strPath, FileMode.CreateNew);
//写数据到a.txt格式
StreamWriter swt = new StreamWriter(fst, System.Text.Encoding.GetEncoding("utf-8"));
//写入
swt.WriteLine(time + strMessage);
swt.Close();
fst.Close();
}
}
public static bool GeException(string strMessage, string strName)
{
string time = System.DateTime.Now.ToString();
string strPathName = System.DateTime.Now.ToShortDateString() + "_" + strName + "_Error__Log.txt";
//指定路径
string strPath = "C:\\EWJAR_LoG";
if (!Directory.Exists(strPath))
{
DirectoryInfo di = Directory.CreateDirectory(strPath);
}
strPathName = strPath + "\\" + strPathName;
WirteFile(strPathName, strMessage);
////如果文件a.txt存在就打开,不存在就新建 .append 是追加写
//FileStream fst = new FileStream(strPathName, FileMode.Append);
////写数据到a.txt格式
//StreamWriter swt = new StreamWriter(fst, System.Text.Encoding.GetEncoding("utf-8"));
////写入
//swt.WriteLine(time + '\r' + "错误信息开始:" + strMessage);
//swt.Close();
//fst.Close();
return true;
}
/// <summary>
/// 判断文件否存在,不存在,创建,存在追加重写
/// </summary>
/// <param name="strPath">文绝对路径</param>
/// <param name="strMessage">消息</param>
public static void WirteFile(string strPath, string strMessage)
{
string time = System.DateTime.Now.ToString();
//判断文件的存在
if (File.Exists(strPath))
{
//存文件
//如果文件a.txt存在就打开,不存在就新建 .append 是追加写
FileStream fst = new FileStream(strPath, FileMode.Append);
//写数据到a.txt格式
StreamWriter swt = new StreamWriter(fst, System.Text.Encoding.GetEncoding("utf-8"));
//写入
swt.WriteLine(time + strMessage);
swt.Close();
fst.Close();
}
else
{
//不存在文件
FileStream fst = new FileStream(strPath, FileMode.CreateNew);
//写数据到a.txt格式
StreamWriter swt = new StreamWriter(fst, System.Text.Encoding.GetEncoding("utf-8"));
//写入
swt.WriteLine(time + strMessage);
swt.Close();
fst.Close();
}
}
相关文章推荐
- 使用openFileInput和openfileOutput方法读写应用程序数据文件夹下的文件
- 2014-03-31工作日志:判断文件是否存在可以使用GetFileAttributes 方法。
- c#FileStream文件读写.以及filestream,file和FileInfo的区别
- php 使用file_get_contents读取大文件的方法
- CFile和CStdioFile的文件读写使用方法
- MBCS工程使用GetOpenFileNameW方法防止文件乱码
- 【转】 FileStream读写文件【StreamWriter 和 StreamReader】
- CFile与CStdioFile的文件读写使用方法详解
- 使用FileStream读写文件
- CFile和CStdioFile的文件读写使用方法
- 使用python读写CSV文件的三种方法
- ASP.NET同时上传多个文件,和不使用HTMLFileInput上传的方法(resource about file upload)
- C# FileStream、StreamWriter、StreamReader读写文件时BOM头问题
- VC++使用ADO方法读写Microsoft Access本地数据库文件(不用ODBC)
- FileStream读写文件【StreamWriter 和 StreamReader】
- log4j配置日志文件log4j.appender.R.File相对路径方法
- 使用UpdateFile文件生成软件注意事项-----运维日志20
- 文件上传ajaxFileUpload.js使用方法 包含后台接收方法
- PHP使用内置函数file_put_contents写入文件及追加内容的方法
- CFile和CStdioFile的文件读写使用方法