开发者

error : this file use by another user

开发者 https://www.devze.com 2023-02-19 21:23 出处:网络
XMl <?xml version=\"1.0\" encoding=\"utf-8\"?> <Questions> <Question> <Id>1</Id>

XMl

<?xml version="1.0" encoding="utf-8"?>
<Questions>
   <Question>
   <Id>1</Id>
   <Text>aaaa</Text>
</Question>
   <Question>
    <Id>2</Id>
    <Text>bbb</Text>
   </Question>
</Questions>

Code

var doc = XDocument.Load(Server.MapPath(".") + "\\Questions.xml");
var elements = from element in doc.Descendants("Question")
               select new
               {
                   Id = element.Element("Id").Value,
                   Text = e开发者_JS百科lement.Element("Text").Value,
                   Reserver = element.Element("Reserver") != null
               };

When a project with the same time I open Mozilla and IE8 this error my xml files in use by another user, but of course sometimes gives this error and sometimes no error

EDIT

If two users request the same time give the display a page that uses XML. In this case, both at the same time I want an XMl open that gives error this file use by another user


If you want to share it between users then you can load it in shared mode:

using (FileStream fs = File.Open(Server.MapPath(".") + "\\Questions.xml", FileMode.Open, FileAccess.Read, FileShare.Read)) {
  var doc = XDocument.Load(fs);
  // ... rest of your code...
}

(Note: Untested code.)

Using FileAccess and FileShare you can regulate how you want shared access to happen. For instance in this example the file can only be opened for shared reading, no writing can occur. If you want to allow writing you need to specify FileShare.ReadWrite. But then the file can change as you read it.

If this is under ASP.Net (I assume since you have multiple users) you may want to consider caching the file in memory on first read.


I assume that what you are looking for is a way to see if the file is allready used by another user, and therefor locked and you get an error. Heres a question that shows how to check if a file is open

0

精彩评论

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