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 .

5
  • is it throwing the exception after the DirectoryInfo line or the _xmlpath one? Commented May 24, 2012 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") Commented May 24, 2012 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? Commented May 24, 2012 at 12:04
  • Also, please post the full exception, and the exact line the exception happens. Commented May 24, 2012 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. Commented May 24, 2012 at 12:16

6 Answers 6

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");
Sign up to request clarification or add additional context in comments.

2 Comments

Worked like a charm :)
My system can't get the Network after Utilities. Any idea! plz share
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()

Comments

2

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

1 Comment

Same Thing i used before But no Success
1

Identity impersonate has done the trick for me

......

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

Comments

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.

2 Comments

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 .
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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.