1

When I wtite a code like below one,

protected void Page_Load(object sender, eventAgrs e)
{
  string xDoc ="Durgesh kumar rao";
  File.Create(Server.MapPath("~/XML"));
  File.WriteAllText(Server.MapPath("~/XML"), xDOC);
}

This throw exception,

The process cannot access the file, because it is being used by another process.

Am I doing something wrong, or File Static Method preform security check i.e why it throws exception.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • 1
    _"Am I doing something wrong"_ - yes, you dump code and an exception message without trying to understand the message or showing what you have done. There's literally a million webpages containing the phrase _"The process cannot access the file, because it is being used by another process"_, of which [this one](http://stackoverflow.com/questions/2781357/file-being-used-by-another-process-after-using-file-create) explains your problem and the solution. – CodeCaster Jul 23 '13 at 10:27
  • +1 as it's a valid question, if someone didn't realize that `Create()` would open a file handle long enough to prevent it's usage. – ericosg Jul 23 '13 at 10:30
  • 2
    @ericosg just curious, why would you upvote (or answer) a question that shows no research effort and that clearly has multiple duplicates? I don't think _"if someone didn't realize"_ is a reason, because you can start to realise stuff once you try to understand it and/or do research for it. – CodeCaster Jul 23 '13 at 10:34
  • indeed exact duplicates are pointless, but I was stating that it's not intuitive to realize, perhaps even if researched. – ericosg Jul 25 '13 at 14:51

3 Answers3

4

File.Create creates the file and returns an open FileStream around it, so you already have a reference to a file handle which is locking it exclusively. In any case, you should be disposing of the result, and in any other case, you should not be discarding return values of calls.

So, in short, dispose of your creation:

using (FileStream stream = File.Create(path)) {

}

However, File.WriteAllText will create the file if it doesn't exist so your call to Create is, other than problematic, redundant. And that doesn't return a 'lingering' stream, it just does the thing and that's it, so just use:

File.WriteAllText(Server.MapPath("~/XML"), xDOC);
Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
4

File.WriteAllText will create the file for you if it does not already exist, so you do not need to call File.Create first. You are getting the error because File.Create creates and opens the file; the file is still open when you call File.WriteAllText.

Polyfun
  • 9,479
  • 4
  • 31
  • 39
3

The File.Create method will return a FileStream giving you access to write to the file. You aren't using it. So the file is open and locked to you for writing and thus, your next statement falls over.

However, you don't need the File.Create at all:

http://msdn.microsoft.com/en-us/library/ms143375.aspx

Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.

File.WriteAllText will handle the creation for you, for free (but it is overwritten, so there's a caveat).

Arran
  • 24,648
  • 6
  • 68
  • 78