如何使用FileSystemWatcher监视对特定属性的更改? [英] How do I watch for a change to a specific attribute using the FileSystemWatcher?

查看:67
本文介绍了如何使用FileSystemWatcher监视对特定属性的更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要监视只读文件的更改.也就是说,我希望在用户将只读文件设置为可写时得到通知.该文件夹同时包含可写和只读状态.文件.我看到FileSystemWatcher有一个NotifyFilter 可以设置为属性",但不能设置为只读"之类的特定属性.在Changed事件中,我们作为参数获取的ChangeType也不会告诉我们哪个属性已更改.我不能只读取文件的当前属性,因为 我不知道活动前是否为只读.有什么方法可以实现我想要的?

I have a requirement to watch for changes to read-only files. That is, I want to be notified when user makes a read-only file writable. The folder contains both writable and read-only files. I see the FileSystemWatcher has a NotifyFilter that can be set to Attributes, but not a specific attribute like "Read-only". In the Changed event, the ChangeType that we get as arguments also doesn't tell us which attribute changed. I can't just read the current attribute of the file because I don't know if it was read-only before the event. Is there any way to achieve what I want?

推荐答案

您好 Priyadarshini Gorur,

Hi Priyadarshini Gorur,

FileSystemWatcher类侦听文件系统更改通知并引发事件 目录或目录中的文件更改时.

FileSystemWatcher Class listens to the file system change notifications and raises events when a directory, or file in a directory, changes.

>> 我看到FileSystemWatcher具有一个NotifyFilter,可以将其设置为Attributes,但不能设置为"Read-only"之类的特定属性.

>> I see the FileSystemWatcher has a NotifyFilter that can be set to Attributes, but not a specific attribute like "Read-only".

是的,FileSystemWatcher无法监视您的只读"磁盘.文件.

Yes, FileSystemWatcher cannot monitor your "read-only" file.

>>我不能只读取文件的当前属性,因为我不知道该文件在事件发生之前是否为只读.是否有任何方法可以实现我想要的目标?

>>I can't just read the current attribute of the file because I don't know if it was read-only before the event. Is there any way to achieve what I want?

请结帐System.IO.FileInfo.IsReadOnly.以下方法可以帮助您检查特定文件是否为只读文件.如果要监视文件夹,则可以使用循环.

Please checkout System.IO.FileInfo.IsReadOnly.  The following method  can help you check if a specific file is read-only or not.  If you want to monitor a folder, you could use a loop.

  // Returns wether a file is read-only.
        public static bool IsFileReadOnly(string FileName)
        {
            // Create a new FileInfo object.
            FileInfo fInfo = new FileInfo(FileName);

            // Return the IsReadOnly property value.
            return fInfo.IsReadOnly;

        }

最诚挚的问候,

克里斯汀


这篇关于如何使用FileSystemWatcher监视对特定属性的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆