现有文本文件(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的方式,我读取文件的时候会不会影响测试系统向文件里写信息?
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的方式,我读取文件的时候会不会影响测试系统向文件里写信息?
System.IO.FileShare可以指定其他程序的读写
在向数据库中插入时将文件copy成一个要操作的临时文件,这样就不会影响原文件的操作了
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
string targetPath = @"D:\Temp\Source\" + DateTime.Now.Date.ToString("yyyyMMdd") + "_Testing-01-A_" + mDateTime + ".log";因为加了mDateTime所以路径就失效了,改一下就可以了。