文件系统观察者代码 [英] file system watcher code
本文介绍了文件系统观察者代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在处理这个基本上运行的文件监视器,如果从这个目录G:\ excelsheet \autoupdates检测到更新,它将复制新的/修改过的文件,并将一个副本粘贴到这个目录C:\ Importantdocs\pnls。由于某种原因,代码中的13行存在编译错误(粗体)。有人可以帮忙吗?
使用System;
使用System.Collections.Generic;
使用System.IO;使用System.Reflection
;
使用System.Text;
使用System.Security.Permissions;
名称空间FancyListener
{
class FileWatcher
{
FileSystemWatcher Watcher;
string Directory =G:\ GMM \Public\FinPro;
[PermissionSet(SecurityAction.Demand,Name =FullTrust)]
public FileWatcher(string Directory)
{
Watcher = new FileSystemWatcher(Directory);
Watcher.Path =目录;
Watcher.NotifyFilter = NotifyFilters.LastWrite;
Watcher.Created + = new FileSystemEventHandler(OnCreated);
//开始观看。
Watcher.EnableRaisingEvents = true;
wait();
}
private void OnChanged(对象源,FileSystemEventArgs e)
{
//指定更改,创建或删除文件时执行的操作。
Console.WriteLine(File:+ e.FullPath ++ e.ChangeType);
int filename_parameter = 1;
copy(filename_parameter);
}
private void OnCreated(对象源,FileSystemEventArgs e)
{
//指定更改,创建或删除文件时执行的操作。
Console.WriteLine(File:+ e.FullPath ++ e.ChangeType);
int filename_parameter = 2;
copy(filename_parameter);
}
private void wait()
{
Console.WriteLine(Waiting);
while(true)
{
}
}
public void copy(int testval)
{
DateTime valueDate = DateTime.Today.Date;
string dateString = valueDate.ToString(dd-MM-yyyy);
string filename_var;
if(testval == 1)
filename_var =rate_file_changed_+ @dateString;
else
filename_var =new_rate_file_+ @dateString;
string fileName = filename_var;
string sourcePath = @G:\ GMM \Public\FinPro;
string targetPath = @\\eitsvrtwprsvc03.ibg.adroot.bmogc.net\Test_DP $;
fileName = System.IO.Directory.GetFiles(sourcePath);
string fileName_start = System.IO.Path.GetFileName(fileName);
string destFile = System.IO.Path.Combine(targetPath,fileName_start);
System.IO.File.Copy(fileName_start,destFile,true);
}
}
}
解决方案
;
fileName = System.IO .Directory.GetFiles(sourcePath);
string fileName_start = System.IO.Path.GetFileName(fileName);
string destFile = System.IO.Path.Combine(targetPath,fileName_start);
System.IO.File.Copy(fileName_start,destFile,true);
}
}
}
你需要要么像这样逃避\:
\\
或
在字符串的前面添加一个@。你可以在页面上用其他几个字符串文件路径完成它。
除了解决方案1中发现的错误之外,我认为你应该知道这些CodeProject文章中描述的一些FileSystemWatcher
问题:
FileSystemWatcher - Pure Chaos(第1部分,共2部分) [ ^ ],
FileSystemWatcher - Pure Chaos(第2部分,共2部分) [ ^ ]。
< DD> -SA
I''ve been working on this file watcher that will basically run and when if detects an update from this directory "G:\excelsheet\autoupdates" it will copy the new/modified files, and paste a copy into this directory "C:\Importantdocs\pnls". For some reason there is a compile error with respect to the 13 line in the code (bolded). Can someone please help?
using System; using System.Collections.Generic; using System.IO; using System.Reflection; using System.Text; using System.Security.Permissions; namespace FancyListener { class FileWatcher { FileSystemWatcher Watcher; string Directory ="G:\GMM\Public\FinPro"; [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] public FileWatcher(string Directory) { Watcher = new FileSystemWatcher(Directory); Watcher.Path = Directory; Watcher.NotifyFilter = NotifyFilters.LastWrite; Watcher.Created += new FileSystemEventHandler(OnCreated); // Begin watching. Watcher.EnableRaisingEvents = true; wait(); } private void OnChanged(object source, FileSystemEventArgs e) { // Specify what is done when a file is changed, created, or deleted. Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType); int filename_parameter = 1; copy(filename_parameter); } private void OnCreated(object source, FileSystemEventArgs e) { // Specify what is done when a file is changed, created, or deleted. Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType); int filename_parameter = 2; copy(filename_parameter); } private void wait() { Console.WriteLine("Waiting"); while (true) { } } public void copy(int testval) { DateTime valueDate = DateTime.Today.Date; string dateString = valueDate.ToString("dd-MM-yyyy"); string filename_var; if (testval == 1) filename_var = "rate_file_changed_" + @dateString; else filename_var = "new_rate_file_" + @dateString; string fileName = filename_var; string sourcePath = @"G:\GMM\Public\FinPro"; string targetPath = @"\\eitsvrtwprsvc03.ibg.adroot.bmogc.net\Test_DP$"; fileName = System.IO.Directory.GetFiles(sourcePath); string fileName_start = System.IO.Path.GetFileName(fileName); string destFile = System.IO.Path.Combine(targetPath, fileName_start); System.IO.File.Copy(fileName_start, destFile, true); } } }
解决方案
"; fileName = System.IO.Directory.GetFiles(sourcePath); string fileName_start = System.IO.Path.GetFileName(fileName); string destFile = System.IO.Path.Combine(targetPath, fileName_start); System.IO.File.Copy(fileName_start, destFile, true); } } }
You need to either escape the "\" like this:
"\\"
or
add an "@" at the front of the string. You have it done with several other string file paths on your page.
In addition to the bug spotted in Solution 1, I think you should be aware of someFileSystemWatcher
problem described in these CodeProject articles:
FileSystemWatcher - Pure Chaos (Part 1 of 2)[^],
FileSystemWatcher - Pure Chaos (Part 2 of 2)[^].
—SA
这篇关于文件系统观察者代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文