- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我开发了一个filewatcher程序来监控一个文件夹,如果文件有任何变化,它会把文件复制到另一个文件夹。
但是我发现在写入原始文件时会出现错误信息(例如文件正在被另一个应用程序处理......)似乎在运行[System.IO.File.Copy]复制到另一个文件夹时文件被锁定.
有什么解决方案可以避免原始文件被filewatcher/System.IO.File.Copy锁定?谢谢。
以下是我的代码:
private void fileWatcher_Changed(object sender, System.IO.FileSystemEventArgs e)
{
DateTime lastWriteTime = File.GetLastWriteTime(e.FullPath);
if (lastWriteTime != lastRead)
{
txtLog.Text += e.ChangeType + ": " + e.FullPath + "\r\n";
txtLog.Focus();
txtLog.Select(txtLog.TextLength, 0);
txtLog.ScrollToCaret();
try
{
string myPath = e.FullPath;
string myFile = e.Name;
System.IO.FileInfo myFileInfo = new System.IO.FileInfo(myFile);
string myAttibs = myFileInfo.Attributes.ToString();
System.IO.File.Copy(myPath, @"D:\\Folder\\Output\\" + myFile, true);
lastRead = lastWriteTime;
}
catch (System.IO.IOException ex)
{
System.IO.IOException myex = ex;
}
catch (System.Exception ex)
{
System.Exception myex = ex;
}
}
}
最佳答案
我遇到了同样的问题。我不喜欢我的解决方案,因为它感觉很老套。但它有效:
FileSystemWatcher fsWatcher = new FileSystemWatcher();
fsWatcher.Created += new FileSystemEventHandler( fsWatcher_Created );
private void fsWatcher_Created( object sender, FileSystemEventArgs e )
{
RaiseFileFoundEvent( e.FullPath );
while ( !TestOpen( e.FullPath ) ) ;
RaiseFileCopyDoneEvent( e.FullPath );
}
private bool TestOpen( string filename )
{
try
{
FileStream fs = new FileStream( filename, FileMode.Open,
FileAccess.Write, FileShare.None );
fs.Close();
return true;
}
catch ( Exception )
{
return false;
}
}
private void RaiseFileFoundEvent( string fullPath )
{
// a file is found, but the copy is not guaranteed to be finished yet.
}
private void RaiseFileCopyDoneEvent( string fullPath )
{
// the file is found, and we know the copy is done.
}
关于c# - 如何在 System.IO.File.Copy 期间避免 Filewatcher 锁定文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10639298/
我正在使用 .net filewatcher 监视某个文件夹以获取某些类型的文件 (*.mbxml)。我正在使用 filewatcher 创建的事件。一旦创建的事件触发,我必须将此文件移动到另一个文件
下面是我编写的文件观察器类的代码: class FileWatcher { #region Method that begins watching public static v
我正在构建一个应用程序,我将在其中查看文件。我对编程很陌生,所以我(也许)没有什么问题,因为我没有真正的实践。我的程序可以正常工作,但我不知道在“真实”软件中通常是如何实现的。请概述和评论。因为我喜欢
尝试编译 .less 时我做错了什么文件到 .css使用 PHPStorm 文件观察器? 这是屏幕截图,(请打开 THIS LINK 以查看图像的完整尺寸): 我安装了 npm install -g
在 Java 中,我正在使用介绍的过程 here 。当我与文件交互时一切正常,但在处理文件夹时遇到问题。该代码确实检测到已添加/修改了新文件夹等,但它没有告诉我这是一个文件夹而不是文件。 它会说“fo
我想用 powershell 查看文件夹,并且我是 PS 初学者。 当我启动脚本时,该脚本只能运行一次。 但是当我因为更改了一些脚本代码而必须再次重新启动脚本时,我收到此错误消息: Cannot su
我正在尝试将多线程与java中的FileWatcher服务集成。即,我不断地监听特定目录 -> 每当创建新文件时,我需要生成一个处理该文件的新线程(假设它打印文件内容)。我设法编写了一个可以编译并运行
我制作了一个 FileWatcher,但我的 FileWatcher 没有按计划工作,我卡住了。 我想要一个可以处理 2 个 map 的文件观察器。 我自己做的代码不会选择我说他应该选择的路径。 在我
我想知道是否在给定目录中创建了新文件。我有下一个代码: private static void CreateWatcher() { //Create a new FileSys
我在我的 winform 应用程序中使用 filewatcher 控件。我正在通过处理其事件来执行上传任务。 现在的问题是,当我在目录(在 FileWatcher 控制下监视)中创建任何新文档时,上传
我是 Rx 的新手,希望在我当前的项目中使用它。我正在尝试实现一个文件观察器系统。至少现在我只对文件创建事件感兴趣。但是当我试图在代码下面运行时,我收到“值不能为空错误消息”。请有人帮我解决以下代码。
这是来自 FileWatcher java 7 nio 库的代码片段。它是非阻塞代码吗?该线程等待来自文件系统的信号。 for (;;) { // wait for key to be sig
我想在 IntelliJ 中使用 SCSS 文件。我看到 tutorial我试着跟着它。但出于某种原因,这对我不起作用。 我做了什么: (我有一台mac电脑)我下载节点 sass 库:npm inst
第一次发布长期读者。 我在 Windows 窗体应用程序中构建了一个工作文件观察器,在将其移动到 Windows 服务之前 100% 正常运行,现在我收到两个单独的问题。此文件观察器读取平面文件以进行
我正在使用 .net 2.0 filewatcher 来监视文件夹中的新文件。它工作得很好,除非我一次放置超过 80 个文件。该事件不再触发。就好像 filewatcher 被设置为跟踪一定数量的文件
我在尝试使用 WebStorm LESS File Watcher 时遇到此错误 /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/
在使用 WPF 应用程序时,我遇到了一个非常疯狂的问题,该应用程序使用 Singleton 实例模式来确保只有一个实例在运行。单个实例检测和命令行转发机制工作正常,但是,作为在辅助实例上退出的启动代码
几天前我开始使用 Rider,我想开始在我的元素中使用 SCSS 并将它们编译回 CSS,我正在摆弄文件观察器,但我无法让它工作,我一直收到这个当我将“显示控制台”设置为“始终”时,控制台中会显示消息
几天前我开始使用 Rider,我想开始在我的元素中使用 SCSS 并将它们编译回 CSS,我正在摆弄文件观察器,但我无法让它工作,我一直收到这个当我将“显示控制台”设置为“始终”时,控制台中会显示消息
我开发了一个filewatcher程序来监控一个文件夹,如果文件有任何变化,它会把文件复制到另一个文件夹。 但是我发现在写入原始文件时会出现错误信息(例如文件正在被另一个应用程序处理......)似乎
我是一名优秀的程序员,十分优秀!