本帖最后由 lj2002aaa188 于 2012-06-23 21:48:15 编辑

解决方案 »

  1.   

    Created 事件方法里 e.FullPath 就是文件路径
    private static void OnCreated(object source, FileSystemEventArgs e)
    {
        Console.WriteLine(e.FullPath);
        RedData(e.FullPath);
    }
      

  2.   

    好的十分感谢,我是编程初学者,知道filesystemeventargs有这个路径属性,就是不知道这个路径就是相应文件的路径。还有就是想请教一下,在OnCreated事件里对txt文件进行读、写操作和删除复制操作会出现异常吗?如果有,这个应该怎么处理?
      

  3.   

    读、写操作和删除 可能会出现异常,但是对于 txt ,这概率极低,可以忽略不计,程序里对这部分加上 try catch ,catch 中将异常信息记录下日志就可以了,如果是准确度要求很高的场合,再根据程序整体逻辑设计补救措施
      

  4.   

    我oncreated 用了你写的2行代码,以前自己写的都去掉了。在调试时出现问题,有时第一次运行正常,第二次就出 错,有时第一次也出错。 错误提示有以下2种:1、 未处理 NullReferenceException  未将对象引用设置到对象的实例2、未处理 IOExecption 文件“d:\......” 正由另一进程使用,因此该进程无法访问该文件。出错后,手动关闭运行窗口,资源管理器中"文件监控入库.vshost.exe"进程仍在,需要强制结束。
      

  5.   


    public static void RedData(string filepath)
            {
                //读取单个文件  测试
                StreamReader sr = new StreamReader(filepath );//出现错误的行!!!!
                while (sr.ReadLine ()!=null )
                {
                    
                    Console .WriteLine (sr.ReadLine ().ToString ());//现错误的行!!!!
                }
                sr.Close();
                //将读取的资料写入数据库
                //............
            }
      

  6.   

    RedData 写法也不对,ReadLine 是立即返回的,不能 while 条件里调用一次,内部再调用一次,这样不仅漏掉一半,而且很可能条件里能返回,循环内就结束返回 null 了,这时再调用 ToString 就 NullReferenceException  异常,假如文件大,可能写的那边还没完,这边就开始读,出现“该进程无法访问该文件”,可以等待并重试一些次数
    public static void RedData(string filepath)
    {
        Stream stream;
        int index = 0;
        while (true)
        {
            try
            {
                stream = File.Open(filepath, FileMode.Open, FileAccess.Read, FileShare.None);
                break;
            }
            catch (Exception)
            {
                if (index++ > 5)
                {
                    throw;
                }
                else
                {
                    Thread.Sleep(1000);
                }
            }
        }    //读取单个文件  测试
        StreamReader sr = new StreamReader(stream);
        string tr = null;
        while ((tr = sr.ReadLine()) != null)
        {
            Console.WriteLine(tr);
        }
        sr.Close();
        stream.Dispose();
        //将读取的资料写入数据库
        //............
    }
      

  7.   

    非常感谢,这个问题已解决。还有一个问题,希望高手在指点一下。创建事件产生后,我要获取txt文件中的数据。但txt文件中的数据行数是可变的,因此我要动态定义数组取接收对用资料。我先按行遍历整个文件,获得文件行数后定义好数组。再重新按行读入,利用切割函数将数据赋值到对应数组。问题是第一次按行读入后,光标位置已到文件尾了,我如何把光标移到文件头,重新开始读一遍呢?还是关闭文件,重新读入。
      对于这种,需要事先获得文本行数的情况,一般是如何处理的? public static void RedData(string filepath)
            {
                Stream stream;
                int index = 0;
                while (true)
                {
                    try
                    {
                        stream = File.Open(filepath, FileMode.Open, FileAccess.Read, FileShare.None);
                        break;
                    }
                    catch (Exception)
                    {
                        if (index++ > 5)
                        {
                            throw;
                        }
                        else
                        {
                            Thread.Sleep(1000);
                        }
                    }
                }            
                //定义接受字段数组
                DateTime[] Ri_Shi;
                string[] SID, Jd_Time, Max_Time;
                float[] T, TMin, TMax, R, P, RH, WdJd, WsJd, WdMax, WsMax, Td;
                //读取单个文件
                StreamReader sr = new StreamReader(filepath );
                int n,num;
                n = 0;
                while (  sr.ReadLine()!= null)
                {
                    n++;
                }
                num=(n-1)/3;
                Ri_Shi = new DateTime[num];
                SID = new string[num];
                Jd_Time = new string[num];
                Max_Time = new string[num];
                T = new float[num];
                TMin = new float[num];
                TMax = new float[num];
                R = new float[num];
                P = new float[num];
                RH = new float[num];
                WdJd = new float[num];
                WsJd = new float[num];
                WdMax = new float[num];
                WsMax = new float[num];
                Td = new float[num];
                 //这里需要从新读入文件,获取数据,但光标已在文件尾
                StreamReader sr = new StreamReader(stream);
                string tr = null;
                 //这里需要改成按行读数据,使用切割函数赋值
                while ((tr = sr.ReadLine()) != null)
                {
                    Console.WriteLine(tr);
                }
                sr.Close();
                stream.Dispose();
            }
      

  8.   

    使用 File.ReadAllLines 一次读取所有行
    public static void RedData(string filepath)
    {
        string[] lines;
        int index = 0;
        while (true)
        {
            try
            {
                lines = File.ReadAllLines(filepath);
                break;
            }
            catch (Exception)
            {
                if (index++ > 5)
                {
                    throw;
                }
                else
                {
                    Thread.Sleep(1000);
                }
            }
        }
        //定义接受字段数组
        DateTime[] Ri_Shi;
        string[] SID, Jd_Time, Max_Time;
        float[] T, TMin, TMax, R, P, RH, WdJd, WsJd, WdMax, WsMax, Td;
        //读取单个文件    int num = (lines.Length - 1) / 3;
        Ri_Shi = new DateTime[num];
        SID = new string[num];
        Jd_Time = new string[num];
        Max_Time = new string[num];
        T = new float[num];
        TMin = new float[num];
        TMax = new float[num];
        R = new float[num];
        P = new float[num];
        RH = new float[num];
        WdJd = new float[num];
        WsJd = new float[num];
        WdMax = new float[num];
        WsMax = new float[num];
        Td = new float[num];    //这里需要改成按行读数据,使用切割函数赋值
        foreach (var item in lines)
        {
            Console.WriteLine(item);
        }
    }