无法使用共享驱动器在 ASP.NET 页面上列出文件或目录内容

发布于 2024-12-12 04:46:07 字数 2883 浏览 0 评论 0 原文

在这个问题中,我使用的是 Visual Studio 2008。我的代码非常简单,因为它是从我从网上获取的参考文献中使用的。我在 Windows Server 2003 机器上使用 ASP/VB.Net、IIS6.0。

我在网上查看了各种来源,但无法拼凑出正确的结果。

这样做的目的是列出大量目录及其各自的文件,并允许用户最终管理该目录(例如打开和删除文件)。

这是我的 ASPX 页面:

<%@ 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

启动页面时,出现以下错误:

'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. 在我的本地开发计算机上,我创建了相同的路径 - 相同的错误

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枕花眠 2024-12-19 04:46:07

在这里您可以找到问题第一部分的答案,为什么 Server.MapPath 不能(也不应该)用于解析网络共享的路径:http://forums.asp.net/t/785846.aspx/1

您评论中提到的第二个问题是如何列出目录中的所有目录:

myDir.GetDirectories()

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文