Windows 7 无法使用StreamReader对象使用Windows服务读取两个连续文件

Windows 7 无法使用StreamReader对象使用Windows服务读取两个连续文件,windows-7,windows-services,streamreader,filesystemwatcher,Windows 7,Windows Services,Streamreader,Filesystemwatcher,我需要能够使用由Windows服务中的FileSystemWatcher处理的StreamReader读取文件行 我已经在网上阅读并尝试了所有有意义的东西,但仍然不起作用。当我连接到我的Windows服务进程(使用Visual Studio 2010的本地计算机)时,整个过程工作得完美无缺 当我试图运行它(在我的本地机器上)而不附加到它并调试它时,第二个文件永远无法通过,我得到以下消息: “进程无法访问文件'C:\Projects\Data\VendingStats\20121213_AZM_t

我需要能够使用由Windows服务中的FileSystemWatcher处理的StreamReader读取文件行

我已经在网上阅读并尝试了所有有意义的东西,但仍然不起作用。当我连接到我的Windows服务进程(使用Visual Studio 2010的本地计算机)时,整个过程工作得完美无缺

当我试图运行它(在我的本地机器上)而不附加到它并调试它时,第二个文件永远无法通过,我得到以下消息: “进程无法访问文件'C:\Projects\Data\VendingStats\20121213_AZM_travely_MIS.txt',因为它正被另一进程使用。”我没有在我的计算机上的任何其他位置打开此文件。它只是在一个目录中。然后我将其复制到一个目录中,FSW接管(以及下面的代码)

有人能告诉我,我需要做什么才能让它工作吗?我不知道为什么当我连接并调试它时它工作得很好,但是当我发送文件时没有连接并调试它时它就不工作了。我觉得这显然是我本地盒子上的东西,我需要禁用,等等——我不知道

我注意到错误甚至在进入“using”语句之前就发生了,因为第二个文件从未被复制到temp目录中进行处理

在StackTrace中,我注意到以下错误: system.io.\uuu error.winioerror(int32 errorcode字符串可能是完整路径)

这是我的密码:

protected override void OnStart(string[] args)
        {
            FileSystemWatcher Watcher = new FileSystemWatcher(@"C:\Projects\Data\VendingStats");
            Watcher.EnableRaisingEvents = true;
            Watcher.Created += new FileSystemEventHandler(Watcher_Created);
            Watcher.Filter = "*.txt";
            Watcher.IncludeSubdirectories = false;
        }

private void Watcher_Created(object sender, FileSystemEventArgs e)
        {
            try
            {
                string targetPath =  @"C:\Temp\VendorStats";

                // Use Path class to manipulate file and directory paths. 
                FileInfo fi = new FileInfo(e.FullPath); // full name of path & file in the FSW directory
                string destFile = Path.Combine(targetPath, fi.Name);

                // To copy a folder's contents to a new location: 
                // Create a new target folder, if necessary. 
                if (!Directory.Exists(targetPath))
                    Directory.CreateDirectory(targetPath);

                // To copy a file to another location and  
                File.Copy(e.FullPath, destFile, true);

                // Set attribute to READONLY
                if (fi.IsReadOnly == false)
                    fi.Attributes = FileAttributes.ReadOnly;

                GetCruiseLineShipName(destFile, ref cruiseLine, ref shipName);

                using (StreamReader sr = new StreamReader(File.Open(destFile, FileMode.Open, FileAccess.Read, FileShare.Read)))
                {
                    filename = e.FullPath;

                    //How many lines should be loaded?
                    int NumberOfLines = 39;

                    //Read the number of lines and put them in the array
                    for (int i = 1; i < NumberOfLines; i++)
                    {
                        ListLines[i] = sr.ReadLine();
                        switch (i)
                        {
                            case 3:
                                int idx = ListLines[i].IndexOf(":");
                                string timeLine = ListLines[i].Substring(idx + 1);
                                dt = GetDate(Convert.ToDateTime(timeLine.Substring(1)));
                                break;

                            //more code here of the same                        
                        }
                    }
                    //InsertData into database                }
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry("VendorStats", "Error in the Main:" + "\r\n\r\n" + ex.Message + "\r\n\r\n" + ex.InnerException);
                return;
            }
        }
protected override void OnStart(字符串[]args)
{
FileSystemWatcher Watcher=新的FileSystemWatcher(@“C:\Projects\Data\VendingStats”);
Watcher.EnableRaisingEvents=true;
Watcher.Created+=新文件系统EventHandler(Watcher_Created);
Watcher.Filter=“*.txt”;
Watcher.IncludeSubdirectories=false;
}
已创建私有无效观察者(对象发送者、文件系统目标)
{
尝试
{
字符串targetPath=@“C:\Temp\VendorStats”;
//使用Path类操作文件和目录路径。
FileInfo fi=newfileinfo(e.FullPath);//FSW目录中路径和文件的全名
字符串destFile=Path.Combine(targetPath,fi.Name);
//要将文件夹内容复制到新位置,请执行以下操作:
//如有必要,创建新的目标文件夹。
如果(!Directory.Exists(targetPath))
目录.CreateDirectory(targetPath);
//将文件复制到其他位置并
Copy(例如FullPath、destFile、true);
//将属性设置为只读
如果(fi.IsReadOnly==false)
fi.Attributes=FileAttributes.ReadOnly;
GetCruiseLineShipName(destFile,ref cruiseLine,ref shipName);
使用(StreamReader sr=new StreamReader(File.Open(destFile,FileMode.Open,FileAccess.Read,FileShare.Read)))
{
filename=e.FullPath;
//应该加载多少行?
int NumberOfLines=39;
//读取行数并将其放入数组中
对于(int i=1;i
解决这个问题的底线是将方法(由FileSystemWatcher生成)休眠“X”秒,直到Windows完全释放上一个和当前文件以及文件夹的资源

实际上是文件系统监视者控制了资源

以下是一些示例代码:

private static void Watcher_Created(object sender, FileSystemEventArgs e)
        {
            try
            {
                Thread.Sleep(10000);

                GetCruiseLineShipName(e.FullPath, ref cruiseLine, ref shipName);

                using (StreamReader sr = new StreamReader(File.Open(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read)))
                { 

我认为我需要将系统帐户添加到管理组,因为这是我调试时运行的帐户,它工作正常。当我在调试之外运行它时,我不是在系统帐户下运行的。如何将系统帐户添加到管理组?