How to access a network network drive file using C # code

In manual mode, I have a network drive connected Y: // Into my system .Drive has Manny Folders files, each of which contains a separate XMl file that has the same folder as the folder.

Here I am trying to read an Xml file from a network location. But this exception The directory was not found . Below is the code I use for this.

Fname = txtwbs.Text; DirectoryInfo objDir = new DirectoryInfo("Y:\\"); \\Y:\\ protected void ImageButton1_Click(object sender, ImageClickEventArgs e) { _xmlpath = objDir + "\\" + Fname + "\\" + Fname + ".xml"; if (File.Exists(_xmlpath )) { reader(_xmlpath); } } 

Here, Fname is the name of the folder and the name of the Xml. Any user enters a file name.

+4
source share
6 answers

Grab this code: http://pastebin.com/RZnydz4Z

Then on Application_Start of your global.asax put this:

 protected void Application_Start(object sender, EventArgs e) { Utilities.Network.NetworkDrive nd = new Utilities.Network.NetworkDrive(); nd.MapNetworkDrive(@"\\server\path", "Z:", "myuser", "mypwd"); } 

Then just use a regular network drive, for example:

 File.ReadAllText(@"Z:\myfile.txt"); 
+10
source

You have this post with asp.net and asp-classic tags. From your sample code, I assume asp-classic is not applicable.

If you are running ASP.Net, the system will not know about the mapped drive you created. Instead, you should use the UNC path. If you are not using a Windows authentication site, you will also need to impersonate someone who has access to this resource, as your anonymous user will most likely receive Access Denied errors.

Finally, I don't think you need a DirectoryInfo call - use Path.Combine ()

+3
source

Ideally, you should use a UNC path to access files. \\server\share\path\to\file

+2
source

True impersonation helped

 ...... <system.web> <identity impersonate="true" userName="yourdomain\yourusername" password="yourpassword" /> ...... ...... 
+1
source

Use this API. http://www.codeproject.com/Articles/6847/Map-Network-Drive-API

This is the only way I was able to do this in code. Just change the class to fit your project.

0
source

Use the UNC path, not the network drive, and make sure that the user running your application has permissions to access the folder and its contents. "File not found" may indicate incorrect permissions or the path is simply incorrect.

0
source

Source: https://habr.com/ru/post/1414124/


All Articles