问题:用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的数据项跟新成功!
通过单步调试,发现每条日志它都有插入文本里,但是在循环下一条信息的时候直接覆盖上一条的,请大家看看程序哪来有问题,谢谢。

解决方案 »

  1.   

    写入时设置append方式。否则每次写都是覆盖。参考:   
        
      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;   
      }   
      }  
      

  2.   

    sw=new StreamWriter("D:\\bank\\LogTxt"+System.DateTime.Now.ToString("yyyyMMdd"),true,System.Text.Encoding.GetEncoding("gb2312")); 
      

  3.   


    仔细看看,StreamWriter的构造函数。
      

  4.   

    有好几种方式,找到自己所需的,用一个方法时不一定要把所有的参数都搞明白,但当你调用时没有达到你要的效果时就要考虑参数的问题了。希望对你以后调用api有所帮助。
      

  5.   

    仔细看看,StreamWriter的构造函数。
      

  6.   


    哈哈后面跟的参数是应该是true,我没注意看到这点,刚看了下msdn,谢谢各位指教。
      

  7.   


    #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
      

  8.   

    Append
    每次打开文件时,使用追加的方法,写到文件尾