我FileSystemWatcher在Form1_Load中添加了这样的:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
....................
Dim watcher As New FileSystemWatcher()
'For watching current directory
watcher.Path = "/"
'For watching status.txt for any changes
watcher.Filter = "status.txt"
watcher.NotifyFilter = NotifyFilters.LastWrite
watcher.EnableRaisingEvents = True
AddHandler watcher.Changed, AddressOf OnChanged
End Sub
Run Code Online (Sandbox Code Playgroud)
我有一个OnChanged函数,它是一个简单的MessageBox.但是,当我更改status.txt文件时,不会显示任何消息框.
考虑以下代码:
string dir = Environment.CurrentDirectory + @"\a";
Directory.CreateDirectory(dir);
FileSystemWatcher watcher = new FileSystemWatcher(dir);
watcher.IncludeSubdirectories = false;
watcher.EnableRaisingEvents = true;
Console.WriteLine("Deleting " + dir);
Directory.Delete(dir, true);
if (Directory.Exists(dir))
{
Console.WriteLine("Getting dirs of " + dir);
Directory.GetDirectories(dir);
}
Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)
有趣的是,这会抛出一个UnauthorizedAccessException Directory.GetDirectories(dir).
删除监视的目录会返回而不会出现错误,但Directory.Exists()仍然返回true并且仍然列出该目录.此外,访问目录会为任何程序产生"拒绝访问".一旦带有FileSystemWatcher的.NET应用程序退出目录就消失了.
如何在仍允许正确删除目录的同时观看目录?
.NET FileSystemWatcher的Changed事件MSDN文档说:
当对正在监视的目录中的文件或目录的大小,系统属性,上次写入时间,上次访问时间或安全权限进行更改时,将引发Changed事件.
但是,当我尝试使用此类捕获对目录或文件的NTFS安全更改时,Changed事件永远不会触发.
有没有一些方法可以在没有民意调查的情况下完成
当有一个大文件移入监视文件夹时,它created甚至在文件被完全复制之前就会引发事件.
在创建的事件中复制此类文件会导致"另一个进程正在使用该文件"错误.
我使用了一个尝试复制文件的线程,直到允许这样做.但我仍然不满意.
我们可以配置FileSystemWatcher,只有在文件完全复制后才会引发创建的事件吗?谢谢.
嗨,我正在创建一个Windows服务来查看某些目录,以查看目录的大小是否达到其限制.我创建了一个文件系统观察器如下
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = dirPaths[i].ToString();
watcher.NotifyFilter = NotifyFilters.Size;
watcher.EnableRaisingEvents = true;
watcher.Changed += new FileSystemEventHandler(OnChanged);
Run Code Online (Sandbox Code Playgroud)
和
private void OnChanged(object source, FileSystemEventArgs e)
{
try
{
string directory = new DirectoryInfo(e.FullPath).Parent.FullName;//gettting the directory path from the full path
float dirSize = CalculateFolderSize(directory);
float limitSize = int.Parse(_config.TargetSize);//getting the limit size
if (dirSize > limitSize)
{
eventLogCheck.WriteEntry("the folloing path has crossed the limit " + directory);
//TODO: mail sending
}
}
catch (Exception ex)
{
eventLogCheck.WriteEntry(ex.ToString());
}
}
Run Code Online (Sandbox Code Playgroud)
CalculateFolderSize检查驱动器中所有文件和子目录的大小. …
默认情况下,在PHPStorm 6中,编译的*.js创建在与*.ts文件相同的文件夹中.
我需要的是编译的*.js和*.d.js在与.ts文件相同的子目录中.
简历中:
我有根文件夹"Project"
我有一个子文件夹"ts",所有*.ts文件都驻留在其中.
我有一个子文件夹"脚本",我想要编译的*.js和*.d.js.
如何使用PHPStorm TypeScript文件观察器完成此操作?
我在File Watcher对话框的Output字段中尝试了几个宏,如:
$FileParentDir$/scripts/$FileNameWithoutExtension$.js:$FileParentDir$/scripts/$FileNameWithoutExtension$.js.map
Run Code Online (Sandbox Code Playgroud)
但他们都没有工作.
我有以下代码用于监视文本文件的目录,该目录每天两次获取新文件,代码工作正常,但之后它停止触发OnCreated事件...
[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
public static void Run()
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"c:\users\documents\";
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Filter = "*.txt";
// Add event handlers.
watcher.Created += new FileSystemEventHandler(OnCreated);
// Begin watching.
watcher.EnableRaisingEvents = true;
// Wait for the user to quit the program.
Console.WriteLine("Press \'q\' to quit the sample.");
while(Console.Read()!='q');
}
private static void OnCreated(object source, FileSystemEventArgs e)
{
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}
Run Code Online (Sandbox Code Playgroud)
无法弄清楚这个问题.
此外,我想知道一个简单的替代品(如果有的话),因为我没有找到这个可靠..
当我尝试监视网络路径上的文件夹(DFS - 分布式文件系统)时,我收到异常System.IO.Internal.BufferOverflowException:一次更改许多更改.当FileSystemWatcher监视不使用此文件系统的本地/网络路径时,它可以正常工作.
我能够从本地路径上的1000 +文件中获取一个事件而我没有得到BufferOverflow异常,但是当我将文件复制到DFS上的文件夹时,我甚至无法从一个文件中获取事件(为了澄清这一点,我收到了错误事件...).
我已经尝试过设置:
fileSystemWatcher.InternalBufferSize = 65536;
Run Code Online (Sandbox Code Playgroud)
我不确定这是否会对你有所帮助,但路径看起来像这样:
\\corpnet\cloud\\Network\testFolder\myFolderToMonitor
Run Code Online (Sandbox Code Playgroud)
编辑:1我不知道为什么路径中有两个双斜杠.我可以在没有问题文件夹的情况下进行监控,直到\ corpnet\cloud路径.一旦我试图监视任何开始的文件夹,我就会收到错误
...\\Network\...
Run Code Online (Sandbox Code Playgroud)
你的任何提示都表示赞赏.
谢谢
c# filesystemwatcher buffer-overflow microsoft-distributed-file-system windows-server-2008
如何将C#中的文件写入FileSystemWatcher监视的文件夹路径?
我的fileSystemWatcher设置如下:
public FileSystemWatcher CreateAndExecute(string path)
{
Console.WriteLine("Watching " + path);
//Create new watcher
FileSystemWatcher fileSystemWatcher = new FileSystemWatcher();
fileSystemWatcher.Path = path;
fileSystemWatcher.IncludeSubdirectories = false;
fileSystemWatcher.NotifyFilter = NotifyFilters.LastWrite |
NotifyFilters.FileName | NotifyFilters.DirectoryName;
fileSystemWatcher.Filter = "*.txt";
fileSystemWatcher.Changed += new FileSystemEventHandler(OnChange);
fileSystemWatcher.InternalBufferSize = 32768;
//Execute
fileSystemWatcher.EnableRaisingEvents = true;
}
private void OnChange(object source, FileSystemEventArgs e)
{
//Replace modified file with original copy
}
Run Code Online (Sandbox Code Playgroud)
我希望每当文件发生未经授权的写入(程序外)时,用数据库中的备份副本替换修改后的文件内容.
但是,当我使用File.WriteAllText()写入修改后的文件时,它会触发FileSystemWatcher的Change事件,因为操作再次注册为write.
这导致程序在覆盖刚刚编写的文件的无限循环中运行.
如何在不触发FileSystemWatcher写入的其他事件的情况下,将修改后的文件替换为备份副本?
基本上,我有这个程序检查目录中的更改,如果有任何内容被复制或创建到所述目录中,则会发生一些事情.这很好用.但是,如果在程序启动时监视目录不为空,我希望代码能够触发,这可能吗?如果是这样,怎么样?