问题:用StreamReader循环读取一条文本信息进行处理后插入数据库,并且用StreamWriter相应的在一个日志文件里写入操作信息,但是问题在于只得到的最后一条信息的日志,前面循环的数据的日志没有保存到文本里,请各位帮忙看下是否程序写的有问题,初学习C#,请指教。
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("D:\\bank");//指定目录
foreach (System.IO.FileInfo fi in dir.GetFiles())//循环返回当前目录的所有列表
{ if (fi.Extension == ".txt")//获取扩展名
{
System.IO.StreamReader sr = new System.IO.StreamReader(fi.FullName);//使用字节流获取文件完整路径
StreamWriter sw = null;
string content = sr.ReadToEnd();//得到文件的所有内容
string str="";//得到每行字段内容
int i=0;
string LKX="",LYEFSE="",YXDWDM="",YSZL="",JJKMDM="",XEXJFSE=""; foreach (string s in content.Split('\n'))//通过换行键,得到每一行信息
{
i++;
str=s+"<br />";
string[] arr = str.Split(',');//取逗号做字符串分离
if(i==1)//特殊处理:只有第一行数据字段数大其后续行
{
YXDWDM=arr[3].Trim();//预算单位代码
YSZL=arr[5].Trim();//预算种类
}
else
{
LKX=arr[0].Trim();//功能科目代码
JJKMDM=arr[1].Trim();//经济科目代码
LYEFSE=arr[2].Trim();//零余额发生额
XEXJFSE=arr[3].Trim();//小额现金发生额:暂时不用
}
bool ExecUpdateReal=false;
bool ExecInsertReal=false;
if(!LKX.Equals(""))
{//当科目代码不为空的时候在进行数据库操作
double JE=double.Parse(LYEFSE);//零余额发生额
double ZEAM=LyezeDouble(YXDWDM,LKX);//得到数据库中现有的:零余额总额
sw=new StreamWriter("D:\\bank\\LogTxt"+System.DateTime.Now.ToString("yyyyMMdd"),false,System.Text.Encoding.GetEncoding("gb2312"));
if(ZEAM>0)
{
ExecUpdateReal=ExecUpdate(ZEAM,JE,YXDWDM,LKX);//执行跟新操作
StreamReal(sw,"ExecUpdateReal",ExecUpdateReal,YXDWDM,LKX,JE);//写操作日志
}
else
{
ExecInsertReal=ExecInsert(YXDWDM,LKX,YSZL,JJKMDM,JE);//执行插入操作
StreamReal(sw,"ExecInsertReal",ExecInsertReal,YXDWDM,LKX,JE);//写操作日志
}
}
}
sr.Close();
Label1.Text="操作完成...请查看日志文档....";
}
}
}====================
private static void StreamReal(StreamWriter sw,string shifou,bool boolreal,string YXDWDM,string LKX,double JE)
{
try
{
if(shifou.Equals("ExecUpdateReal"))
{
if(boolreal.Equals(true))
{
sw.WriteLine(System.DateTime.Now.ToString("yyyyMMdd hh:mm:ss")+"预算单位代码"+YXDWDM+"功能科目代码"+LKX+"的金额为"+JE+"的数据项跟新成功!");
}
else
{
sw.WriteLine(System.DateTime.Now.ToString("yyyyMMdd hh:mm:ss")+"预算单位代码"+YXDWDM+"功能科目代码"+LKX+"的金额为"+JE+"的数据项跟新失败!");
}
}
else if(shifou.Equals("ExecInsertReal"))
{
if(boolreal.Equals(true))
{
sw.WriteLine(System.DateTime.Now.ToString("yyyyMMdd hh:mm:ss")+"预算单位代码"+YXDWDM+"功能科目代码"+LKX+"的金额为"+JE+"的数据项插入成功!");
}
else
{
sw.WriteLine(System.DateTime.Now.ToString("yyyyMMdd hh:mm:ss")+"预算单位代码"+YXDWDM+"功能科目代码"+LKX+"的金额为"+JE+"的数据项插入失败!");
}
}
sw.Flush();
}
catch (IOException e)
{
Console.WriteLine(e.StackTrace);
}
finally
{
sw.Close();
}
}得到的日志:20090302 11:13:56预算单位代码000000057010功能科目代码2130105的金额为20500的数据项跟新成功!
通过单步调试,发现每条日志它都有插入文本里,但是在循环下一条信息的时候直接覆盖上一条的,请大家看看程序哪来有问题,谢谢。
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("D:\\bank");//指定目录
foreach (System.IO.FileInfo fi in dir.GetFiles())//循环返回当前目录的所有列表
{ if (fi.Extension == ".txt")//获取扩展名
{
System.IO.StreamReader sr = new System.IO.StreamReader(fi.FullName);//使用字节流获取文件完整路径
StreamWriter sw = null;
string content = sr.ReadToEnd();//得到文件的所有内容
string str="";//得到每行字段内容
int i=0;
string LKX="",LYEFSE="",YXDWDM="",YSZL="",JJKMDM="",XEXJFSE=""; foreach (string s in content.Split('\n'))//通过换行键,得到每一行信息
{
i++;
str=s+"<br />";
string[] arr = str.Split(',');//取逗号做字符串分离
if(i==1)//特殊处理:只有第一行数据字段数大其后续行
{
YXDWDM=arr[3].Trim();//预算单位代码
YSZL=arr[5].Trim();//预算种类
}
else
{
LKX=arr[0].Trim();//功能科目代码
JJKMDM=arr[1].Trim();//经济科目代码
LYEFSE=arr[2].Trim();//零余额发生额
XEXJFSE=arr[3].Trim();//小额现金发生额:暂时不用
}
bool ExecUpdateReal=false;
bool ExecInsertReal=false;
if(!LKX.Equals(""))
{//当科目代码不为空的时候在进行数据库操作
double JE=double.Parse(LYEFSE);//零余额发生额
double ZEAM=LyezeDouble(YXDWDM,LKX);//得到数据库中现有的:零余额总额
sw=new StreamWriter("D:\\bank\\LogTxt"+System.DateTime.Now.ToString("yyyyMMdd"),false,System.Text.Encoding.GetEncoding("gb2312"));
if(ZEAM>0)
{
ExecUpdateReal=ExecUpdate(ZEAM,JE,YXDWDM,LKX);//执行跟新操作
StreamReal(sw,"ExecUpdateReal",ExecUpdateReal,YXDWDM,LKX,JE);//写操作日志
}
else
{
ExecInsertReal=ExecInsert(YXDWDM,LKX,YSZL,JJKMDM,JE);//执行插入操作
StreamReal(sw,"ExecInsertReal",ExecInsertReal,YXDWDM,LKX,JE);//写操作日志
}
}
}
sr.Close();
Label1.Text="操作完成...请查看日志文档....";
}
}
}====================
private static void StreamReal(StreamWriter sw,string shifou,bool boolreal,string YXDWDM,string LKX,double JE)
{
try
{
if(shifou.Equals("ExecUpdateReal"))
{
if(boolreal.Equals(true))
{
sw.WriteLine(System.DateTime.Now.ToString("yyyyMMdd hh:mm:ss")+"预算单位代码"+YXDWDM+"功能科目代码"+LKX+"的金额为"+JE+"的数据项跟新成功!");
}
else
{
sw.WriteLine(System.DateTime.Now.ToString("yyyyMMdd hh:mm:ss")+"预算单位代码"+YXDWDM+"功能科目代码"+LKX+"的金额为"+JE+"的数据项跟新失败!");
}
}
else if(shifou.Equals("ExecInsertReal"))
{
if(boolreal.Equals(true))
{
sw.WriteLine(System.DateTime.Now.ToString("yyyyMMdd hh:mm:ss")+"预算单位代码"+YXDWDM+"功能科目代码"+LKX+"的金额为"+JE+"的数据项插入成功!");
}
else
{
sw.WriteLine(System.DateTime.Now.ToString("yyyyMMdd hh:mm:ss")+"预算单位代码"+YXDWDM+"功能科目代码"+LKX+"的金额为"+JE+"的数据项插入失败!");
}
}
sw.Flush();
}
catch (IOException e)
{
Console.WriteLine(e.StackTrace);
}
finally
{
sw.Close();
}
}得到的日志:20090302 11:13:56预算单位代码000000057010功能科目代码2130105的金额为20500的数据项跟新成功!
通过单步调试,发现每条日志它都有插入文本里,但是在循环下一条信息的时候直接覆盖上一条的,请大家看看程序哪来有问题,谢谢。
解决方案 »
- 求助一个C# WinForm下,关于System.OutOfMemoryException的一个问题
- C#怎么打开文件?
- 散分,请教。。。。
- 请问 使用C#如何新建环境变量 谢谢
- winform做一个聊天功能,怎么实现!
- C#对Excel的图表操作!(关于x轴的绑定。。。谢谢!)
- C#中窗体缩放后,如何调整其中的很多button的排列?
- webservices具有推数据的能力吗?
- 如何在打印超过打印机最大的长度?
- c#难不难?我是用PB编程的,学这个容易吗?它与vc++有何区别?
- 加载多个对象,怎么能不引起长时间以及闪烁的问题?
- c# 中ftp 编程中,判断ftp 服务器上某个目录是否存在的命令是什么?^_^
public bool WriteLogFile( string _FilePath, string _FileContent )
{
try
{
System.IO.FileStream oFileStream;
if ( ! System.IO.File.Exists( _FilePath ) )
{
oFileStream = new System.IO.FileStream( _FilePath, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite );
}
else
{
oFileStream = new System.IO.FileStream( _FilePath, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite );
}
System.IO.StreamWriter oStreamWriter = new System.IO.StreamWriter( oFileStream, System.Text.Encoding.Default );
oStreamWriter.Write( _FileContent );
oStreamWriter.WriteLine();
oStreamWriter.Close();
oFileStream.Close();
return true;
}
catch( System.Exception ex )
{
System.Web.HttpContext.Current.Response.Write( ex.ToString() );
return false;
}
}
仔细看看,StreamWriter的构造函数。
哈哈后面跟的参数是应该是true,我没注意看到这点,刚看了下msdn,谢谢各位指教。
#region 写日志文件
string FilePath = @"D:\SynDataLog";
if (!Directory.Exists(FilePath))
{
Directory.CreateDirectory(FilePath);
} string fname = FilePath + "\\" + DateTime.Now.ToString("yyyyMMdd").ToString() + ".txt";
string input = "\r\n数据同步日志\r\n源服务器:" + conn1_HJZx + "\r\n目标服务器:" + conn2_HJZx + "\r\n" + Lost + "同步开始时间:" + dt.ToString() + ",同步结束时间:" + DateTime.Now.ToString(); ///创建写数据流,并设置追加。
StreamWriter w = new StreamWriter(fname, true); ///写入日志内容并换行
w.Write(input + "\r\n"); ///清空缓冲区内容,并把缓冲区内容写入基础流
w.Flush(); ///关闭写数据流
w.Close();
#endregion
每次打开文件时,使用追加的方法,写到文件尾