现有文本文件(a.log)
TR014 ver 1.1;loading;090202;134424;
08090950446;0 263 004 329;090202;134519;
08090950598;0 263 004 329;090202;134830;
08090950597;0 263 004 329;090202;134910;第一行是测试系统开始信息,第二行开始是测试的具体日志(以分号为间隔)。现在我需要用windows服务每隔5分钟(同步读取好像比较复杂)读取此文件的信息,并且插入数据库。
现在的问题是,在第二次读取文件的时候没法定位。
比如:
第一次文件一共123行,但是五分钟后此文件可能有321行,我如何在第二次的时候从124行开始去读数据?请大家帮忙出出主意。
顺便问下,如果用stream的方式,我读取文件的时候会不会影响测试系统向文件里写信息?

解决方案 »

  1.   

    你在服务里记录你个变量来保存当前获取的行数..
    System.IO.FileShare可以指定其他程序的读写
      

  2.   

    可以设置一个计数器来记录已经读取过的总行数
    在向数据库中插入时将文件copy成一个要操作的临时文件,这样就不会影响原文件的操作了
      

  3.   

    1.方法有很多,(1)向配置文件等记录当前的访问结束位置, 下次从这个位置+1行去访问.(2)每次读完以后在日志文件的最后位置插入一个结束标志, 下次开始的时候搜索定位这个结束标志然后删除.2.如果用stream的方式,我读取文件的时候会不会影响测试系统向文件里写信息?看你以什么方式创建这个文件流, 你现在只是读取的话, 可以设定访问属性. 我觉得或多或少还是影响, 所以你可以考虑拷贝啊之类的方法, 或者访问文件的过程一定要短
      

  4.   

    谢谢各位,如果我用同步的方式需要怎么样,用filewatcher好像不怎么好,而且占资源。
      

  5.   


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Xml;
    using System.Configuration;
    using System.Data;
    using System.Diagnostics;
    using System.ServiceProcess;
    using System.Text;
    using System.IO;
    using System.Threading;namespace DataTransfer
    {
        public partial class Service1 : ServiceBase
        {
            public Service1()
            {
                InitializeComponent();
                if (!System.Diagnostics.EventLog.SourceExists("DataTransferSource"))
                {
                    System.Diagnostics.EventLog.CreateEventSource("DataTransferSource", "DataTransfer");
                }
                bw = new BackgroundWorker();
                bw.DoWork += new DoWorkEventHandler(bw_DoWork);
                bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
            }        protected override void OnStart(string[] args)
            {
                string mDateTime = DateTime.Now.ToString();
                eventLog1.WriteEntry("DataTransfer Service was started at " + mDateTime);
                bw.RunWorkerAsync(args);        }        protected override void OnStop()
            {
                string mDateTime = DateTime.Now.ToString();
                bw.CancelAsync();
                while (bw.IsBusy) Thread.Sleep(300000);
                eventLog1.WriteEntry("DataTransfer Service was stopped at " + mDateTime);
            }        void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                string mDateTime = DateTime.Now.ToString();
                eventLog1.WriteEntry("DataTransfer Service was completed at " + mDateTime);
            }        void bw_DoWork(object sender, DoWorkEventArgs e)
            {
                while (!bw.CancellationPending)
                {
                    string mDateTime = DateTime.Now.ToString();
                  //  Trace.WriteLine("DataTransfer: " + new Random().Next().ToString());
                  //  eventLog1.WriteEntry("DataTransfer Service was running at " + mDateTime + "with content " + new Random().Next().ToString());
                    CopyFile();    // 上面被注释的代码可以执行,但是调用CopyFile()就没办法执行了,日志中追查不到,我看了下文件夹权限问题,应该也不是,因为targetpath的权限是everyone可写,ASPNET可写
                    Thread.Sleep(300000);
                }
            }
            public void CopyFile()
            {
                string mDateTime = DateTime.Now.ToString();
                string sourcePath = @"D:\Temp\Source\" + DateTime.Now.Date.ToString("yyyyMMdd") + "_Testing-01-A.log";
                string targetPath = @"D:\Temp\Source\" + DateTime.Now.Date.ToString("yyyyMMdd") + "_Testing-01-A_" + mDateTime + ".log";
                try
                {
                    if (!File.Exists(sourcePath))
                    {
                        eventLog1.WriteEntry("The file" + sourcePath + " was not existed at " + mDateTime);
                        return;
                    }
                    if (File.Exists(targetPath))
                    {
                        eventLog1.WriteEntry("The file" + targetPath + " was existed at " + mDateTime);
                        return;
                    }
                    File.Copy(sourcePath, targetPath);
                    eventLog1.WriteEntry("The file was copied to the target path at " + mDateTime);
                }
                catch (Exception ee)
                { }
            }    }
    }请问这代码哪出问题了,为什么windows服务没办法跑,主要是我不知道windows服务如何debug
      

  6.   

    找到问题所在了:
    string targetPath = @"D:\Temp\Source\" + DateTime.Now.Date.ToString("yyyyMMdd") + "_Testing-01-A_" + mDateTime + ".log";因为加了mDateTime所以路径就失效了,改一下就可以了。