开发者

File listener - how can I know when the file is accessable?

开发者 https://www.devze.com 2023-01-23 03:29 出处:网络
I have a f开发者_如何学编程ile listener on a win service. I raise event on file created/ some time I get execption \"cannot access to the file because it being used by another program\"

I have a f开发者_如何学编程ile listener on a win service.

I raise event on file created/

some time I get execption "cannot access to the file because it being used by another program" do someone has an idea?


You could use FileSystemWatcher to get notification on the file system but you should be more explicit if possible.

More info on it here: http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx


My suggestion, even when using a FileSystem watcher, is simply to do a Thread.Sleep(1000) after the FileCreated (or whichever event you are watching) has fired, so that you are 100% sure that the previous process has done with it. I have had a similar experience when using FileSystem watchers, and pausing for a while before trying the access the file fixed the issue.

0

精彩评论

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