Solved

Getting files from a mapped directory in ASP.Net

Posted on 2004-09-10
22
962 Views
Last Modified: 2008-01-09
I am trying to retrieve the list of files in the directory " Docs/S000700" which is in the d drive of the W1 computer.

I have mapped the D Drive of the W1 computer to my computer on the S:/

I am using the following code to retrieve the file list in that directory but I am getting path not found error at
  Exception Details: System.IO.DirectoryNotFoundException: Could not find a part of the path "S:/Docs/S000700/".


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
             Const IMAGE_DIRECTORY As String = "S:/Docs/S000700/"
        Dim pics As ArrayList = New ArrayList
        Dim s As String, html As String
                ' If Directory.Exists(Server.MapPath(IMAGE_DIRECTORY)) Then
               For Each s In Directory.GetFiles("S:/Docs/S000700/", "*.*")
            html = "<a href=""" & IMAGE_DIRECTORY & Path.GetFileName(s) & """>" & _
                   "<p> """ & Path.GetFileName(s) & """ </p>" & _
                   "</a>"

            pics.Add(html)
        Next

        dlPictures.DataSource = pics
        dlPictures.DataBind()
       ' End If
    End Sub


If I copy the Docs/S000700 folder to the C: of my computer and modify the following lines the code runs fine and it displays the list

 For Each s In Directory.GetFiles("C:/Docs/S000700/", "*.*")



Do I have to use a different syntax when retrieving files from a mapped directory???
Please help
Thanks
0
Comment
Question by:wilkersons
  • 10
  • 5
  • 4
  • +3
22 Comments
 
LVL 8

Expert Comment

by:boulder_bum
ID: 12030074
"Do I have to use a different syntax when retrieving files from a mapped directory???"

Try using the network path:

"\\computer-name/d$/temp/Docs/S000700"
0
 

Author Comment

by:wilkersons
ID: 12030301
I tried adding the network path and it gives me the following error:
Exception Details: System.IO.IOException: Logon failure: unknown user name or bad password.



For Each s In Directory.GetFiles("\\wasolomon\d$\Solomon\Docs\S000700\", "*.*")


When i type the following in Start>Run , it displays the files in  S000700 in windows explorer
\\wasolomon\d$\Solomon\docs/s000700
0
 
LVL 2

Assisted Solution

by:viksi
viksi earned 100 total points
ID: 12030339
The d$ method is only used by administrators on the destination machine. Your ASP user probably will not have such priviledges.

Either way, that is not good practice, unless you're doing it by hand. Instead, create a share on the destination computer, for example "Solomon" and then use "\\wasolomon\Solomon\...".

This way you can control the user privileges and you gain destination transparency as the share can move from one folder to another without breaking your application.
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12030344
You need to make sure your domain user account has a local account on the machine where the mapped drive exists.

Regards,

Aeros
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12030350
you may also need to impersonate identity if you are not already.
0
 
LVL 17

Assisted Solution

by:AerosSaga
AerosSaga earned 200 total points
ID: 12030367

Accepted Answer from jnhorst
Date: 09/01/2004 12:01AM EDT
Grade: A
      Accepted Answer       

OK, I think I see.  I originally thought you were trying to get to files on your PDC box (the one with IIS, right?).  Since you are trying to navigate to another server by way of a UNC path (I assume), what you want to do is create a *domain* account (not a local account on the file server) and give that domain account the necessary privileges for the file share.  Then change the IIS setting to use that domain account to authenticate anonymous requests.  Leave the <identity /> tag in web.config.

I did something like this before where I built a menu system based on the folder tree of a share on the file server and then had links to the files that were in the folders.  I had to have a domain account for anonymous access so I could grant persmission to the file server share.

John
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12030447
all credit goes to jnhorst for this original post....Aeros
0
 
LVL 33

Expert Comment

by:raterus
ID: 12030485
if you are using integrated windows authentication you will actually have to do even more to be able to access the network share.  Delegation
0
 

Author Comment

by:wilkersons
ID: 12030673
Thanks everyone for trying to help me out.

I tried adding <identity impersonate ="true"> and I was able to see the list on the development machine , but when i tried to access the page from the remote machine I got an error???

Well I have few questions

Viksi:
What do you mean by:
 Instead, create a share on the destination computer, for example "Solomon" and then use "\\wasolomon\Solomon\...".

John
can you please explain how can i do this??
what you want to do is create a *domain* account (not a local account on the file server) and give that domain account the necessary privileges for the file share.  Then change the IIS setting to use that domain account to authenticate anonymous requests.

Rateus:
What you mean by integrated windows authentication.
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12030691
1) I'm Aeros I posted some of a previous excerpt from jnhorst.
2) You create the domain account in the MMC you might need a network admin for this depending on your situation.
3) You set the application to run under this account in IIS' Properties for the particular web application and then allow anonymous access using this account.

Regards,

Aeros
0
 

Author Comment

by:wilkersons
ID: 12030824
Hi Aeros,
Sorry abt the confusion.

Lemme check the details abt domain account and will get back to you today or on monday.
Thanks
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:wilkersons
ID: 12030845
What could be the reason for this behavior
I tried adding <identity impersonate ="true"> and I was able to see the list on the 'development machine' , but when i tried to access the page from the 'remote machine' I got an error???

Is it related to the user settings...or the domain account settings??
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12030862
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12030869
probably your running under your user account locally, which has the necessary permissions, but when you publish its running under the default asp.net account presumably, and thus cannot access the share.
0
 
LVL 2

Expert Comment

by:viksi
ID: 12030998
> Viksi:
> What do you mean by:
>  Instead, create a share on the destination computer, for example "Solomon" and then use "\\wasolomon\Solomon\...".

I am not sure what your security restrictions are, but I was recommending a very simple solution. Create a share on the remote computer, if your remote computer is "wasolomon", create a share on that computer. Name it, for example, "Solomon", give permissions on the share to "Everyone" (FOR TESTING ONLY!!!) and then on the source code, use:

For Each s In Directory.GetFiles("//wasolomon/Solomon/", "*.*")

...

That should do the trick. Be aware of security issues with this option however. As I mentioned before, as I don't know your application requirements and details I can only assume.
0
 
LVL 2

Expert Comment

by:viksi
ID: 12031008
By the way, if the above solution will suffice, create a domain user, i.e. ASP_USER, and give that user permissions to access that share on the "wasolomon"...
0
 
LVL 10

Accepted Solution

by:
jnhorst earned 200 total points
ID: 12032304
Wilkersons:

Here the skinny on all of this:  First, use backslashes (\), not forward slashes (/) in your path strings when accessing files in the System.IO namespace.  Definitely use a UNC path (\\ServerName\Share\folder\filename.ext) and not a mapped drive.  You have to remember that what is mapped on your dev box may not be so mapped on your server.  UNC paths take this uncertainty out of the picture.

Now to authentication:  If you bring up the IIS admin utility under Administrative Tools in Control Panel and right click your websites root, check the Directory Security tab and click the Edit button for "Anonymous Access...".  You will see that a certain account is set to authenticate anonymous requests to website.  The default for this is IUSR_<machinename>.  This is a local account on your dev box.  One of two things are happening: 1) if you have <identity impersonate="true" /> in web.config, when you make a request for your web page, that request is authenticated with the IUSR_... account; and  2) Without the <identity> tag, it is authenticated with the ASPNET account.  Both are local to your box.  You can show the S: drive because your personal windows account, which is not a local account but a domain or Active Directory account, has access to the network share in question.  But both the local IUSR_... account and the ASPNET account on your box are completely unknown anywhere else on the network.  So when IIS runs your page request under either IUSR_... or ASPNET and the code tries to access a file share via the UNC path, it will not be allowed.

The solution is to convince your network admin to create a user account on the network domain controller or Active Directory specifically for authenticating anonymous requests.  If you can get this done, go back to the IIS setting for anonymous auth and reset the IUSR_... account name with the name of this new account and enter its password.  Do this on both your dev and production IIS boxes.  Then make sure that this account has permissions to access the file share with the files you are trying to display.

Aeros: I'm honored, dude!  With 200,000 point you're a freak of nature! :-)

John

0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12032656
thank you John, I am honered by your comment;)
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12032664
Your always spot on with the windows user & permissions issues, I learn a lot from your posts
0
 
LVL 2

Expert Comment

by:viksi
ID: 12044134
Jnhorst:

Not that significant, just thought I'd point out. The difference between for-slash and back-slash when using windows, for example, is a convenience matter. You don't have to type http, for example, when you type in // as the windows shell accepts it as a default.

.NET on the other hand will use the appropriate one regardles when you use the System.IO class. Usually the for-slashes are used for convenience as to not have to add double back-slashes (as the back-slash also means special character).
0
 

Author Comment

by:wilkersons
ID: 12045980
Thanks a lot each one of you for your valuable suggestions and comments.
I'll get back after doing some more R&D.

Thanks again
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12105267
Your very welcome my friend.

Regards,

Aeros
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now