virtual directory and hyperlink file path issue (listed and linked paths don't match)

J

Jim in Arizona

Some basic code to display files in a directory, in this case, C:\webfiles

What's interesting is the difference between using system.io.directory and
system.io.directoryinfo classes.
When displaying files simply with directory, I can do this:

Dim filelisting() As String = Directory.GetFiles("C:\webfiles\")
DataGrid1.DataSource = filelisting
DataGrid1.DataBind()

That works just fine, although with no hyperlinks or anything.

When using directoryinfo, I have to do this:

Dim dirinfo As New DirectoryInfo(Server.MapPath(".\webfiles\"))

DataGrid2.DataSource = dirinfo.GetFiles()
DataGrid2.DataBind()

I had originally tried to specifiy Server.MapPath("C:\webfiles\") like with
the directory class but this didn't work. It threw an error saying it was
not a virtual directory. To solve this, I opened IIS up and created a
virtual directory called "webfiles" within the working directory of this
asp.net web form (C:\Inetpub\wwwroot\tests). This virtual directory was
mapped to C:\webfiles. This works fine to display the files, with
hyperlinks, by doing this in the aspx page.

<asp:DataGrid id="DataGrid2" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:HyperLinkColumn DataNavigateUrlField="Name" DataTextField="Name"
HeaderText="File Name" />
</Columns>
</asp:DataGrid>

And, in the aspx.vb code behind page (same as shown above):

Dim dirinfo As New DirectoryInfo(Server.MapPath(".\webfiles\"))

DataGrid2.DataSource = dirinfo.GetFiles()
DataGrid2.DataBind()

This works to display the file names in C:\webfiles with hyperlinks, but
when I click on one, I get a 'page cannot be found' message. When looking at
the path when I hover the mouse over the link, it shows:
http://localhost/tests/myfile.doc

It should be showing:

http://localhost/tests/webfiles/myfile.doc

since webfiles is the virtual directory it is getting the file listing from.
If I directly insert that address into the address bar of the web browser, I
get the file.

Other than just placing all the files into the C:\Inetpub\wwwroot\tests
folder and not worrying about virtual directories, how can I solve this
issue. My current environment is a test environment and in the future, in a
production enironment, the virtual folder would probably be a share on
another machine so I'd like to solve this rather than taking the easy way
out and not messing around with virtual directories.

TIA,
Jim
--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top