0

Ok, before I say anything else, let me note that I know this has been asked multiple times on SO. However, I have scoured the internet and haven't been able to find a solution for my particular case.

I'm trying to take an XmlDocument object and save it to a network drive. It works perfectly when I try to save it to my local C drive, but when I try to save to a network drive, either testing locally or from an IIS7 web server, I get the error "Cannot find part of the path V:\Test\DocName.xml". Here is the code I am using to try and accomplish this:

using (var stream = new FileStream(Path.Combine(@"V:\Test", doc.DocumentElement.Name + ".xml"), FileMode.Create, FileAccess.Write))
    doc.Save(stream);

doc is, of course, an XmlDocument. I have double and triple checked that I have rights to the directory, and I know for a fact that it exists and is not hidden or anything. Any ideas?

2 Answers2

0

AFAIK Drive mappings are per user. See more in this answer.

This means that if Visual Studio is not running under the same account as your mappings, it won't find it.

Also IIS is presumably running under a different "user". So same there.

ispiro
  • 26,556
  • 38
  • 136
  • 291
0

From Using Mapped Drives with IIS

Drive mappings are an extension to the net use command and are created on a per-user basis. That is, the entries for drive mappings exist under the Hkey_Current_User registry key for the user who maps to the network drive. Also, drive mappings are created with the credentials of the user who is currently logged on.

As .NET websites run in an AppPool and this is associated with an account, usually a service account, it will not recognise any drive mappings.

It is recommended in .NET applications to use UNC paths instead of drive mappings.

Philip Smith
  • 2,741
  • 25
  • 32