开发者

Alternatives ways to getting the properties of the file?

Background:

I have a file monitoring service that watches for changes in the files on the local system using the FileSystemWatcher class and i am handling for events like Created,Deleted,Renamed开发者_如何学Go. When these events are triggered,I would simply want to GET THE PROPERTIES OF THE FILE such as FileName,FileSize,CreationTime,LastAccessTime,LastWriteTime using the FileSystemInfo class.

Problem:

While this service is running, I am unable to uninstall some programs for example (Microsoft Security Essentials). I have a feeling that these service is HANGING ON TO THE RESOURCES of the files marked for deletion because I can only uninstall those programs if only this service is running.

My Question is how can I GET THE PROPERTIES OF THE FILE (as specified above) in an ALTERNATIVE & efficient way without hanging on to the resources of the file ?

Here is my code using the FileSystemInfo

public void OnCreate/OnRenamed(object source, FileSystemEventArgs e)

{ FileInfo file = new FileInfo(e.FullPath); 

String output = "<Event><TimeStamp>" + currentTime + "</TimeStamp>";
            output += "<Name>" + action + "</Name>";
            output += "<Properties><FileName>" + file.Name + "</FileName>";
            output += "<FullPath>" + file.FullName + "</FullPath>";
            output += "<FileSize>" + file.Length + "</FileSize>";
            output += "<CreationTime>" + String.Format("{0:yyyyMMdd-HHmmss.fff}", file.CreationTime) + "</CreationTime>";
            output += "<LastAccess>" + String.Format("{0:yyyyMMdd-HHmmss.fff}", file.LastAccessTime) + "</LastAccess>";
            output += "<LastWriteTime>" + String.Format("{0:yyyyMMdd-HHmmss.fff}", file.LastWriteTime = DateTime.Now) + "</LastWriteTime></Properties></Event>";

}

Sincerely, Derek


Using FileSystemInfo will not normally 'hang on' to these files. You have to first figure out what exactly is causing other programs to stuck during uninstallation. Use ProcessMonitor to see what files are being accessed during uninstallation. The tool is pretty self explanatory, you need to filter on file system activity. Read this or google around.

Try to experiment by taking out one thing at a time. I assume these programs get uninstalled successfully when your service is not running. This proves that it is indeed your service that is causing issues. First comment your FileSystemInfo code. See if the problem goes away. Then comment out FileSystemWatcher and see if it helps.

Update: looks like this is offending line:

file.LastWriteTime = DateTime.Now

Try to comment this assignement and see if it solves the problem:

file.LastWriteTime /* = DateTime.Now */

Was this a typo or you really need to write LastWriteTime?


What you could do is every interval check for LastAccessTime and LastWriteTime and do some quick math based on the interval and you can determine if the file was last accessed or modified within the interval time. If it was, do what you have to do. Just an idea...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜