public void OnCreate(object source, FileSystemEventArgs e)
            {
                try
                {
                    StreamWriter sw = new StreamWriter("D:/123/Log.txt", true);
                    sw.WriteLine("File: {0} Created", e.FullPath);
                    kq(e.FullPath);                     sw.Close();
              //this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "Wrote create event to log");
                }
                catch (IOException)
                {
                    this.BeginInvoke(new UpdateWatchTextDelegate(UpdateWatchText), "Error Writing to log");
                }
            }
我想问的是为什么在添加了函数kq之后就不能多次运行onCreate了呢,添加之前是可以的呀,请各位帮忙解答一下

解决方案 »

  1.   

    1.可能kq(e.FullPath);出错了
    2.可能kq(e.FullPath);中有中断事件代码你的描述与代码太少,无法做出准确判断。
      

  2.   

    public void kq(string path)
                {
                  ArrayList arr = new ArrayList();//存储所有字符串                try
                    {
                        System.IO.FileStream aFile = new FileStream(path, FileMode.Open);//打开文件
                        System.IO.StreamReader sr = new StreamReader(aFile, System.Text.Encoding.Default);//得到文件                 
                      while (!sr.EndOfStream)//读到文件尾
                        {
                            string[] row = sr.ReadLine().Replace("\r\n", "a").Split('a');
                            foreach (string r in row)
                            {
                                if (r != "")
                                    arr.Add(r);
                            }
                        }
                    }
                    catch (IOException e)//抛出异常处理
                    {
                        MessageBox.Show("An IO exception has been thrown!");
                        MessageBox.Show(e.ToString());
                        return;
                    }
     
                    string dat;//kqtime
                    dat = (string)arr[1];//数组
                    string time = dat.Substring(21, 11);
                       string dat1
                        dat1 = (string)arr[5];
                        string location1 = dat1.Substring(0, 6);
                         string index1 = dat1.Substring(43, 3);
                         string quality1 = dat1.Substring(55, 4);
                         string wrw1 = dat1.Substring(71);
                         SqlConnection thisConnection1 = new SqlConnection(@"server=PC-PC\SQLEXPRESS;Database=xuzhouqixian;Integrated Security=true;Connection Timeout=30;");
                        thisConnection1.Open();
                        string sql1 = "insert into kongqi (kqtime,kqlocation,kqindex,kqquality,kqwrw) values('" + time + "','" + location1 + "','" + index1 + "','" + quality1 + "','" + wrw1 + "')";
                        //insert into SQL(值)values(列)
                        SqlCommand cmd1 = new SqlCommand(sql1, thisConnection1);
                        cmd1.ExecuteNonQuery();
                        thisConnection1.Close(); //关闭                  
                    
                    MessageBox.Show("数据已添加进数据库!");
                }
    大致上是这样的,是不是因为函数中变量的值未释放干净,所以不能有新值呢,大家帮忙看一下……
      

  3.   

    因为你犯了一个初学者常见错误...文件流是独占非托管资源,使用完必须关闭...而你try catch的处理没有关闭,kq一旦抛异常,再次执行就会文件冲突...用using块把StreamWriter那一段包起来...
      

  4.   

    可是我用using还是不行呀,不知道using的参数怎么设置
      

  5.   

    写个finally里面sw.Dispose()吧
     此消息通过 【CSDN论坛 Winform测试版】 回复!
      

  6.   

    现在我用using块把文件流结束了,为什么还是不行呢,还是会弹出Catch里面的警告
      

  7.   

    补充一下,这个运行成功了一次,也就是说,虽然弹出"An IO exception has been thrown!",但是还是可以继续添加运行,既可以忽视,不知现在为什么又不行了……