0

In this question I'm using Visual Studio 2008. My code is quite simple, as it was used from a reference I grabbed off the web. I'm using ASP/VB.Net, IIS6.0 on a Windows Server 2003 box.

I've looked at various sources online, and have not been able to piece together a proper result.

The purpose of this is to list a slew of directories and their respective files, and allow the user to eventually manage that directory (such as open and delete files).

Here is my ASPX page:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="DirectoryList.aspx.vb" Inherits="VCMReports.DirectoryList" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:DataGrid runat="server" id="articleList" Font-Name="Verdana"
    AutoGenerateColumns="False" AlternatingItemStyle-BackColor="#eeeeee"
    HeaderStyle-BackColor="Navy" HeaderStyle-ForeColor="White"
    HeaderStyle-Font-Size="15pt" HeaderStyle-Font-Bold="True">
  <Columns>
    <asp:HyperLinkColumn DataNavigateUrlField="Name" DataTextField="Name" 
           HeaderText="File Name" />
    <asp:BoundColumn DataField="LastWriteTime" HeaderText="Last Write Time"
        ItemStyle-HorizontalAlign="Center" DataFormatString="{0:d}" />
    <asp:BoundColumn DataField="Length" HeaderText="File Size"
        ItemStyle-HorizontalAlign="Right" 
        DataFormatString="{0:#,### bytes}" />
  </Columns>
</asp:DataGrid>  

    </div>
    </form>
</body>
</html>

And here is the CodeBehind:

Imports System.IO
Imports System.IO.DirectoryInfo

Partial Public Class DirectoryList
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dirInfo As New DirectoryInfo(Server.MapPath("V:\Users\"))

        articleList.DataSource = dirInfo.GetFiles("V:\Users\")
        articleList.DataBind()

    End Sub
    Protected Sub GetFiles()

        Dim files() As String = Directory.GetFiles("V:\Users\")

        Dim myDir As DirectoryInfo = New DirectoryInfo("V:\Users\")
        Dim fileInfos() As IO.FileInfo = myDir.GetFiles("*.*")
    End Sub

When launching the page, the following error appears:

'V:\Users\' is not a valid virtual path. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Web.HttpException: 'V:\Users\' is not a valid virtual path.

Things I have tried:

  1. On the server, I have mapped out the drive and the directory.
  2. Permissions seem to be properly set, impersonate is correct.
  3. If I were to list a directory in the actual directory "C:\Inetpub\Directory\", there is no failure.
  4. I do understand this is incomplete for the Directory listing portion, this will only list files (so this will need to be modified.
  5. I have attempted to create a Virtual Directory, and the application presented the same error.
  6. On my local development machine, I have the same path created - same error.

How am I able to view the contents of a shared directory and it's files?

RogueSpear00
  • 619
  • 2
  • 9
  • 24
  • 2
    http://forums.asp.net/t/785846.aspx/1 – Tim Schmelter Oct 27 '11 at 20:57
  • Removed Server.MapPath, and then changed GetFile("V:\users\") to GetFile("*.*") and was able to successfully retrieve the files. The second part of the question, and I can ask this in a new thread if required, is how can I be able to change this functionality to list directories? – RogueSpear00 Oct 27 '11 at 21:02
  • `For Each dir As DirectoryInfo In myDir.GetDirectories()` – Tim Schmelter Oct 27 '11 at 21:09

1 Answers1

2

Here you find the answer on your first part of the question, why Server.MapPath cannot(and should not) be used to resolve the path to a network share: http://forums.asp.net/t/785846.aspx/1

The second question mentioned in your comment is how to list all directories in a directory:

myDir.GetDirectories()

http://msdn.microsoft.com/en-us/library/s7xk2b58.aspx

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939