如何检测文件何时放入我的文件夹之一

发布于 2024-10-19 05:11:56 字数 128 浏览 1 评论 0原文

在我的软件中,我将在我的文件夹之一中收到一个 XML 文件和一个 PDF 文件,因此我想启动一个事件来读取 XML,以便我可以管理这些文件。

我怎样才能检测到这个事件,你能给我推荐一本关于事件的书或一个页面,也许给我一个例子。

in my software I'm going to receive in one of my folders an XML file and a PDF file so i want to launch an event to read the XML so i can manage this files.

how can i detect this event, can u suggest me a book about events or a page, maybe gimme an example.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

肩上的翅膀 2024-10-26 05:11:56

您想要使用 FileSystemWatcher

void foo()
{
    FileSystemWatcher watcher = new FileSystemWatcher();
    watcher.Path = "C:\\temp";
    watcher.Changed += watcher_Changed;
    watcher.Created += watcher_Created;
    watcher.Filter = "*.pdf";
}

void watcher_Created(object sender, FileSystemEventArgs e)
{
   //handle created
}

void watcher_Changed(object sender, FileSystemEventArgs e)
{
    //Handle changed
}

You want to use FileSystemWatcher .

void foo()
{
    FileSystemWatcher watcher = new FileSystemWatcher();
    watcher.Path = "C:\\temp";
    watcher.Changed += watcher_Changed;
    watcher.Created += watcher_Created;
    watcher.Filter = "*.pdf";
}

void watcher_Created(object sender, FileSystemEventArgs e)
{
   //handle created
}

void watcher_Changed(object sender, FileSystemEventArgs e)
{
    //Handle changed
}
猫性小仙女 2024-10-26 05:11:56

如前所述,FileSystemWatcher可行的方法。

但请注意,它有一些微妙之处:创建的每个文件都会引发一次 Created 事件,但文件首次出现在文件夹中时就会引发该事件。如果您有一个不同的进程来复制该文件(可能是通过网络连接),如果您在处理 Created 事件时尝试访问该文件,则可能会遇到异常。

另请注意,如果另一个进程正在复制您正在观看的文件夹中的文件,则很可能会多次引发 Changed 事件。使用 NotifyFilter 减少引发的事件数量。

as mentioned before, FileSystemWatcher is the way to go.

note, however, that there are a few subtleties to it: the Created event is raised once per file created, but it is raised as soon as the file first appears in the folder. if you have a different process that copies that file, perhaps over a network connection, if you try to access the file while handling the Created event, you may get an exception.

Also note that the Changed event is most likely going to be raised multiple times if another process is copying a file in the folder you're watching. Use the NotifyFilter to reduce the number of events that are raised.

情何以堪。 2024-10-26 05:11:56

您可以使用 FileSystemWatcher 类来监视该文件夹XML 文件。

You can use the FileSystemWatcher class to watch that folder for XML files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文