【问题标题】:Unable to find Directory name找不到目录名称
【发布时间】:2016-05-18 09:49:08
【问题描述】:

我想从路径中获取目录名,但它返回以下错误:

目录名无效

以下是示例路径:

strFilePathName = C:\inetpub\wwwroot\PSSWeb\foldername\xxx.ini

我使用以下代码根据前缀 PSS 获取 Directoryname,它应该返回 PSSWeb。我无法找到解决方案。

var directories = Directory.GetDirectories(strFilePathName,
                                           "PSS?",
                                           SearchOption.AllDirectories);

【问题讨论】:

  • 错误是什么?
  • @Nasreddine "目录名无效"
  • strFilePathName 不是目录名而是文件名
  • var 目录 = Directory.GetDirectories(Directory.GetParent(strFilePathName).FullName,"PSS?",SearchOption.AllDirectories);
  • Directory.GetParent(strFilePathName) 它返回错误的路径..

标签: c# .net window


【解决方案1】:

您指定的路径名​​“C:\inetpub\wwwroot\PSSWeb\foldername\xxx.ini”不是目录而是文件。

    //
    // Summary:
    //     Returns the names of the subdirectories (including their paths) that match the
    //     specified search pattern in the specified directory, and optionally searches
    //     subdirectories.
    //
    // Parameters:
    //   path:
    //     The relative or absolute path to the directory to search. This string is not
    //     case-sensitive.
    //
    //   searchPattern:
    //     The search string to match against the names of subdirectories in path. This
    //     parameter can contain a combination of valid literal and wildcard characters
    //     (see Remarks), but doesn't support regular expressions.
    //
    //   searchOption:
    //     One of the enumeration values that specifies whether the search operation should
    //     include all subdirectories or only the current directory.
    //
    // Returns:
    //     An array of the full names (including paths) of the subdirectories that match
    //     the specified criteria, or an empty array if no directories are found.
    //
    // Exceptions:
    //   T:System.ArgumentException:
    //     path is a zero-length string, contains only white space, or contains one or more
    //     invalid characters. You can query for invalid characters by using the System.IO.Path.GetInvalidPathChars
    //     method.-or- searchPattern does not contain a valid pattern.
    //
    //   T:System.ArgumentNullException:
    //     path or searchPattern is null.
    //
    //   T:System.ArgumentOutOfRangeException:
    //     searchOption is not a valid System.IO.SearchOption value.
    //
    //   T:System.UnauthorizedAccessException:
    //     The caller does not have the required permission.
    //
    //   T:System.IO.PathTooLongException:
    //     The specified path, file name, or both exceed the system-defined maximum length.
    //     For example, on Windows-based platforms, paths must be less than 248 characters
    //     and file names must be less than 260 characters.
    //
    //   T:System.IO.IOException:
    //     path is a file name.
    //
    //   T:System.IO.DirectoryNotFoundException:
    //     The specified path is invalid (for example, it is on an unmapped drive).
    public static string[] GetDirectories(string path, string searchPattern, SearchOption searchOption);

这就是方法的文档。提到第一个参数必须是目录。

【讨论】:

  • 太好了,错误消失了,但是在搜索模式中找不到目录名中的前缀,你知道可能是什么问题吗?
  • 使用 PSS* 代替 PSS?它还会在子目录和父目录中找到文件。
【解决方案2】:

最后我使用与上面完全不同的方法得到了我想要的结果,如下所示:

string strFilePathName = "C:\inetpub\wwwroot\PSSWeb\foldername\xxx.in";
string x = Path.GetDirectoryName(strFilePathName);
DirectoryInfo dir = new DirectoryInfo(@x);
DirectoryInfo OneLevelsUp = dir.Parent;

我从路径C:\inetpub\wwwroot\PSSWeb\foldername\xxx.ini 得到PSSWeb 文件夹名称的结果。

获取二级文件夹名称

如果有人想升级两级,您可以尝试以下线路, 改成

 DirectoryInfo OneLevelsUp = dir.Parent 
 DirectoryInfo TwoLevelsUp = dir.Parent.Parent

它会根据上面提到的路径返回wwwroot文件夹名称

【讨论】:

    猜你喜欢
    • 2018-04-19
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 2013-11-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    相关资源
    最近更新 更多