Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 找不到目录名_C#_.net_Window - Fatal编程技术网

C# 找不到目录名

C# 找不到目录名,c#,.net,window,C#,.net,Window,我想从path获取目录名,但它返回以下错误: 目录名无效 以下是示例路径: strFilePathName=C:\inetpub\wwwroot\PSSWeb\foldername\xxx.ini 我使用以下代码根据前缀PSS获取Directoryname,它应该返回PSSWeb。我找不到解决这个问题的办法 var directories = Directory.GetDirectories(strFilePathName,

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

目录名无效

以下是示例路径:

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

我使用以下代码根据前缀PSS获取
Directoryname
,它应该返回PSSWeb。我找不到解决这个问题的办法

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

指定的路径名“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);

这是该方法的文档。需要指出的是,第一个参数必须是目录。

您指定的路径名“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);

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

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

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
文件夹名

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

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
文件夹名

错误是什么?@nasredine“目录名无效”strFilePathName不是目录名,而是文件名var directories=directory.GetDirectories(directory.GetParent(strFilePathName).FullName,“PSS?”,SearchOption.AllDirectories);Directory.GetParent(strFilePathName)返回错误的路径..错误是什么?@nasredine“目录名无效”strFilePathName不是目录名而是文件名var directories=Directory.GetDirectories(Directory.GetParent(strFilePathName).FullName,“PSS?”,SearchOption.AllDirectories);Directory.GetParent(strFilePathName)返回错误的路径..太好了,错误消失了,但是它无法像在searchpattern中一样在目录名中找到前缀,你知道可能是什么问题吗?使用PSS*而不是PSS?它还会在子目录和父目录中查找文件。很好,错误已经消失,但它无法像在searchpattern中一样在目录名中找到前缀,你知道会出现什么问题吗?使用PSS*而不是PSS?它还会在子目录和父目录中查找文件。