4

Manually I Have Mapped Network Drive Y:// To My System .Drive is having Manny Folders each Containg Single XMl File having Same as Folder .

Here I am Trying to Read Xml File From Network Location . But It is Giving Exception Directory Not Found . Below Code I am Using For that .

                 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 Folder Name Also Xml Name .Whatever User Will Enter the Name of File .

Nicholas Murray
  • 13,305
  • 14
  • 65
  • 84
kuldeep verma
  • 326
  • 2
  • 4
  • 10
  • is it throwing the exception after the DirectoryInfo line or the _xmlpath one? – ewanm89 May 24 '12 at 11:45
  • 1
    Also, for better coding style, try to use `Path.Combine` when constructing Paths. E.g. `Path.Combine(objDir, Fname, Fname + ".xml")` – MichelZ May 24 '12 at 11:49
  • So, your final "path" is `Y:\txtwbsname\txtwbsname.xml` Does that really already exist if the user can supply an arbitrary name for `txtwbs.Text`? – MichelZ May 24 '12 at 12:04
  • Also, please post the full exception, and the exact line the exception happens. – MichelZ May 24 '12 at 12:07
  • yes exactly ..and Directory Not found exception . When i debug the program it is redirecting on same path ..but i am not geeting why it is giving error. If i use path which i get at the time of debgging , i can open directly file using that from Run option. – kuldeep verma May 24 '12 at 12:16

6 Answers6

14

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

Then on the 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 like a normal network drive, like this:

File.ReadAllText(@"Z:\myfile.txt");
boskop
  • 501
  • 6
  • 8
3

You have this post tagged as both asp.net and asp-classic. From your code example, I'm guessing asp-classic doesn't apply.

If you are running in ASP.Net, the system wouldn't know about the mapped drive you've created. You should use the UNC path instead. If you aren't running the site with Windows Authentication, you'll also need to impersonate someone who has access to the share, as your anonymous user most likely will receive "Access Denied" errors.

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

Dave Simione
  • 1,441
  • 2
  • 21
  • 31
2

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

MichelZ
  • 4,214
  • 5
  • 30
  • 35
1

Identity impersonate has done the trick for me

......

 <system.web>
<identity impersonate="true" userName="yourdomain\yourusername" 
    password="yourpassword" />
......
......
Esen
  • 973
  • 1
  • 21
  • 47
0

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

It the only way i have managed to do it in code. Just modify the class, so it fits to you project.

Mr.GT
  • 310
  • 4
  • 12
  • This Solution is Only for mapping . I Already Mapped Drive Manually .. No need For Coding . Only I need to REad File and Folder Stucture from Mapped Drived Or Directly from Shared Folder . – kuldeep verma May 24 '12 at 11:56
  • Yes I know, but use it with a 'using' statement and then read the file inside the using block. This was the only way it allowed me to read files from a network drive together with User Impersonation. – Mr.GT May 24 '12 at 12:04
0

Use a UNC path rather than a network drive and make sure the user running you application has permissions to access the folder and it's contents. "File not found" can mean wrong permissions or the path is just wrong.

willDaBeast
  • 120
  • 6