【问题标题】:How to get sub-folder names from a given path Server.MapPath如何从给定路径 Server.MapPath 获取子文件夹名称
【发布时间】:2011-08-26 07:33:33
【问题描述】:

我想在 ASP.NET MVC 3 应用程序中从 server.MapPath 获取文件夹名称。 在此操作中,我必须检查(给定文件夹名称中是否存在更多文件夹)该文件夹中是否有 .jpg 文件,如果是,则返回该文件夹。

string path = Server.MapPath("Content/");
DirectoryInfo dInfo = new DirectoryInfo(path);
DirectoryInfo[] subdirs = dInfo.GetDirectories();

if (Directory.Exists(path))
{
    ArrayList ar = new ArrayList();
    // This path is a directory
    ar.Add(path);
    //ProcessDirectory(path);
}

【问题讨论】:

  • 您好,感谢您的回复,我试图获取文件夹,但我没有得到解决方案,我已经在我的查询中发布了我的代码,请检查一次......并提供任何建议跨度>

标签: asp.net-mvc-3 c#-4.0 subdirectory


【解决方案1】:

我不确定我是否正确理解了这个问题,但我认为你想要类似的东西

string path = Server.MapPath(YOURPATH);
List<string> files = Directory.GetFiles(path, "*.jpg", SearchOption.AllDirectories);

或类似的东西

string path = Server.MapPath(YOURPATH);
List<string> picFolders = new List<string>();

if(Directory.GetFiles(path, "*.jpg").Length > 0)
    picFolders.Add(path)

foreach(string dir in Directory.GetDirectories(path, "*", SearchOption.AllDirectories))
{
    if(Directory.GetFiles(dir, "*.jpg").Length > 0)
        picFolders.Add(dir)
}

【讨论】:

  • 您好,我正在尝试您的代码,但它显示错误,例如“找不到路径 'D:\victor\Pivot\Ilifelooks\Ilifelooks\Ilifelooks\Gallery\Content\' 的一部分。”
  • 路径“D:\victor\Pivot\Ilifelooks\Ilifelooks\Ilifelooks\Gallery\Content\”是否存在?
  • 是的,它存在于我的服务器路径中的那个文件夹中,一些图像也存在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-25
  • 2011-04-13
  • 2011-07-10
  • 2016-12-29
  • 1970-01-01
相关资源
最近更新 更多