• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 980
  • Last Modified:

Getting files from a mapped directory in ASP.Net

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
wilkersons
Asked:
wilkersons
  • 10
  • 5
  • 4
  • +3
3 Solutions
 
boulder_bumCommented:
"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
 
wilkersonsAuthor Commented:
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
 
viksiCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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

Regards,

Aeros
0
 
AerosSagaCommented:
you may also need to impersonate identity if you are not already.
0
 
AerosSagaCommented:

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
 
AerosSagaCommented:
all credit goes to jnhorst for this original post....Aeros
0
 
raterusCommented:
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
 
wilkersonsAuthor Commented:
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
 
AerosSagaCommented:
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
 
wilkersonsAuthor Commented:
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
 
wilkersonsAuthor Commented:
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
 
AerosSagaCommented:
0
 
AerosSagaCommented:
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
 
viksiCommented:
> 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
 
viksiCommented:
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
 
jnhorstCommented:
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
 
AerosSagaCommented:
thank you John, I am honered by your comment;)
0
 
AerosSagaCommented:
Your always spot on with the windows user & permissions issues, I learn a lot from your posts
0
 
viksiCommented:
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
 
wilkersonsAuthor Commented:
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
 
AerosSagaCommented:
Your very welcome my friend.

Regards,

Aeros
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 10
  • 5
  • 4
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now