如何确定其他进程是否正在使用我的XML文件? [英] How do I determine if another process is using my XML file?
问题描述
我有一个程序,读取该程序,然后(可能)更新XML文件.我使用XMLDocument的Load(filename)方法.
I have a program that reads and then (possibly) updates an XML file. I use the Load(filename) method of XMLDocument.
我的程序允许多个实例,并且两个实例可以尝试对同一文件进行操作.如何防止这种情况?或者,如果另一个程序(例如ASCII编辑器)试图同时更改此文件怎么办?
My program allows multiple instances and it is possible for two instances to try to operate on the same file. How do I prevent this? Or what if another program, such as an ASCII editor, is trying to simultaneously change this file?
在此先感谢您的帮助.
Michael Bate
Michael Bate
推荐答案
使用File.Open(path,FileMode.Open,FileAccess.ReadWrite,FileShare.None)进行放置共享对文件的锁定,然后使用方法的XmlDocument.Load(Stream)版本.
Using File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None) to place share lock to the file and then use the XmlDocument.Load(Stream) version of method instead.
这篇关于如何确定其他进程是否正在使用我的XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!