无法使用共享驱动器列出ASP.NET页上的文件或目录内容 [英] Unable to List File or Directory Contents on ASP.NET Page using Shared Drive

查看:72
本文介绍了无法使用共享驱动器列出ASP.NET页上的文件或目录内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个问题中,我使用的是Visual Studio2008.我的代码非常简单,因为它是从我从网上获取的引用中使用的.我在Windows Server 2003机器上使用的是ASP/VB.Net,IIS6.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).

这是我的ASPX页面:

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>

这是背后的代码:

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.

我尝试过的事情:

  1. 在服务器上,我已映射出驱动器和目录.
  2. 权限似乎设置正确,模拟是正确的.
  3. 如果要在实际目录"C:\ Inetpub \ Directory \"中列出目录,则不会失败.
  4. 我确实知道对于目录列表部分这是不完整的,它只会列出文件(因此需要进行修改.
  5. 我试图创建一个虚拟目录,并且该应用程序出现了相同的错误.
  6. 在我的本地开发计算机上,我创建了相同的路径-相同的错误.

如何查看共享目录及其文件的内容?

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

推荐答案

在这里,您在问题的第一部分找到了答案,为什么

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

这篇关于无法使用共享驱动器列出ASP.NET页上的文件或目录内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆