Link to home
Start Free TrialLog in
Avatar of bergertime
bergertime

asked on

vb.net 2003 get files from folder on c:

I'm using this code to get files from a folder on a virtual directory, how would I change it to get files from c:\mystuff\files

   Dim di As New System.IO.DirectoryInfo(Server.MapPath("/files"))
        For Each fi As System.IO.FileInfo In di.GetFiles()
            Response.Write(String.Format("<a href=""/files/{0}"">{0}</a><br />", fi.Name))
        Next
I'm using vb.net 2003.  Thanks
ASKER CERTIFIED SOLUTION
Avatar of sabeesh
sabeesh
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Use this
Private Function Recur(ByVal strPath As String) 'set byval to desired path
Dim oDir As New System.IO.DirectoryInfo(strPath)
Dim oSubDir() As System.IO.DirectoryInfo
Dim oFiles() As System.IO.FileInfo
Dim i As Int32

oFiles = oDir.GetFiles
For i = 0 To oFiles.Length - 1
 Response.Write(oFiles(i).Name.ToString.SubString(oFiles(i).Name.ToString.Length-4))
Next
End Function
sorry use this
Private Function Recur(ByVal strPath As String) 'set byval to desired path
Dim oDir As New System.IO.DirectoryInfo(strPath)
Dim oSubDir() As System.IO.DirectoryInfo
Dim oFiles() As System.IO.FileInfo
Dim i As Int32

oFiles = oDir.GetFiles
For i = 0 To oFiles.Length - 1
 Response.Write(oFiles(i).Name.ToString)
Next
End Function
You could simply change this line:
Dim di As New System.IO.DirectoryInfo(Server.MapPath("/files"))

To
Dim di As New System.IO.DirectoryInfo("C:\MyStuff\files"))

But I recommend that you follow the directions given by sabeesh for adding it to your config rather than hard coding it.

Note that you may have to add permissions to folder for the ASP.NET worker process user.  It's usually ASPNET, but you can find the name using Environment.UserName .
Avatar of bergertime
bergertime

ASKER

"But I recommend that you follow the directions given by sabeesh for adding it to your config rather than hard coding it."
As I was typing in my question, it dawned on me to try your suggestion, and it works fine......I'm just wondering why you would recommend it that way?  Thanks

>I'm just wondering why you would recommend it that way?  Thanks

Well, just because something works doesn't mean there isn't a better way to do it.  If you were deploying this application to some other environment, and you had to point to a different folder, then if you had hard coded the path, you would have to change the source code, recompile and deploy the new version.  Then the source would no longer be compatible with your development environment.  So as a general rule, it is far better to not hard code path information in your application, but retrieve it either as a relative path (using Server.MapPath for ASP.NET) or as a setting in a config file.
Oh, so just to help me understand.  If I make changes to my config file, I don't have to recompile?  I've always recomplied after making changes to my config file.  Thanks
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial