【问题标题】:Access Virtual Directory folder through code behind Asp.net通过 Asp.net 后面的代码访问虚拟目录文件夹
【发布时间】:2013-02-25 14:09:00
【问题描述】:

我正在尝试从代码隐藏访问虚拟目录文件夹。

  • ASP.Net 网站名称:SuperImages
  • 物理文件夹:C:\images
  • 虚拟目录文件夹:allimages(与 App_Data、Scripts、Properties 文件夹处于同一级别)

我正在尝试访问并计算此文件夹中的项目数,然后将它们显示在网页上。

我应该怎么做?

提前致谢!

================================================ =========================

更新:从下面的帖子看来,Server.MapPath 会给我正确的物理路径。但是,在我看来,我走错了物理路径。原因应该是我正在运行“调试”模式。

因此,知道如何确保 Server.MapPath 正确指向并在调试模式下运行吗?

================================================ ========================

解决方案:

问题是在调试模式下,我使用的是 VS 开发服务器而不是本地 IIS。我在本地 IIS 中为我​​的应用程序重新创建了一个虚拟目录。在这个新创建的应用程序中为“allimages”文件夹重新创建了另一个虚拟目录,它解决了问题。

【问题讨论】:

  • 在调试模式下得到什么路径?
  • @BhushanFirake 我的项目路径:C:\users\juniordeveloper\SuperImages\allimages。
  • 然后尝试用 `\` 更改我的/
  • @BhushanFirake 好的,找到问题了。事实证明,在调试模式下,我使用的是 VS 开发服务器而不是我的本地 IIS。我在本地 IIS 中为我​​的应用程序重新创建了一个虚拟目录。在这个新创建的应用程序中为“allimages”文件夹重新创建了另一个虚拟目录,它解决了问题。感谢 Bhushan 的帮助!

标签: asp.net directory virtual


【解决方案1】:

你可以这样做:

DirectoryInfo dir= new DirectoryInfo(Server.MapPath("/allimages"));

然后你可以得到这个文件夹中的文件如下:

FileInfo[] files = dir.GetFiles(string searchPattern,SearchOption searchOption);

对于文件的数量,您可以简单地进行数组计数。

【讨论】:

  • 嗨,我认为这应该可行。但是我意识到 Server.MapPath 不断返回错误的物理路径。原因应该是我正在运行“调试”模式。因此,知道如何确保 Server.MapPath 正确指向并在调试模式下运行吗?谢谢。
  • @juniordeveloper87 您说您的allimages 文件夹与App_Data 处于同一级别?所以它应该可以工作,否则你的路径可能会有所不同。再次检查。
【解决方案2】:

如帖子中建议的那样
File count from a folder
你可以这样走。

你可以使用

Directory.GetFiles方法

另见Directory.GetFiles Method (String, String, SearchOption)

您可以在此重载中指定搜索选项。

TopDirectoryOnly:在搜索中仅包括当前目录。

AllDirectories:在搜索操作中包括当前目录和所有子目录。此选项包括重新解析点,例如搜索中的已安装驱动器和符号链接。

// searches current directory and sub directory
int fCount = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Length;
// searches current directory
int fCount = Directory.GetFiles(path, "*.*", SearchOption.TopDirectory).Length;

您必须使用Server.MapPath 作为您的虚拟目录文件夹。

  string dir = Server.MapPath(@"/Content/slideshow/images/image");
  FileInfo[] files;
  int numFiles;
  files = (new System.IO.DirectoryInfo(dir)).GetFiles("filePattern");
  numFiles = files.Length;

【讨论】:

    【解决方案3】:

    您可以像从普通应用程序访问它一样访问它。我会使用 Directory 类来计算项目的数量。只要确保您有足够的权限即可。

    Directory.EnumerateFiles(myPath).Length;
    

    【讨论】:

      猜你喜欢
      • 2015-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多