如何获取路径c#中的父目录?
这是我的路径示例如何获取路径c#中的父目录?,c#,C#,这是我的路径示例E:\test\img\sig.jpg 我想获得E:\test\img来创建目录 我试着分开,但它是img 所以我尝试了function Directory.CreateDirectory,路径是E:\test\img\sig.jpg\ 告诉我一个想法?用于返回指定路径字符串的目录信息 string directoryName = Path.GetDirectoryName(filePath); 建议的方法是使用: Path类包含许多有用的路径处理方法,这些方法比手动字符串操作
E:\test\img\sig.jpg
我想获得E:\test\img
来创建目录
我试着分开,但它是img
所以我尝试了function Directory.CreateDirectory,路径是E:\test\img\sig.jpg\
告诉我一个想法?用于返回指定路径字符串的目录信息
string directoryName = Path.GetDirectoryName(filePath);
建议的方法是使用:
Path
类包含许多有用的路径处理方法,这些方法比手动字符串操作更可靠:
var directoryComponent = Path.GetDirectoryName(@"E:\test\img\sig.jpg");
// yields `E:\test\img`
为了完整起见,我想提到Path.Combine
,它的作用正好相反:
var dirAndFile = Path.Combine(@"E:\test\img", "sig.jpg");
// no more checking for trailing slashes, hooray!
要创建目录,可以使用
directory.create
。请注意。另一种解决方案可以是:
FileInfo f = new FileInfo(@"E:\test\img\sig.jpg");
if (f.Exists)
{
string dirName= f.DirectoryName;
}
您可以尝试使用此代码查找目录名。 System.IO.FileInfo fi=new System.IO.FileInfo(@“E:\test\img\sig.jpg”); 字符串dirname=fi.DirectoryName 和创建目录
Directory.CreateDirectory(dirname) 请您编辑您的问题,向我们展示您尝试过的内容,然后我们可以提供帮助。提问前只需谷歌一下,可能会出现重复。您可能需要更改标题。在您的示例中,父目录是:
E:\test
FileInfo f = new FileInfo(@"E:\test\img\sig.jpg");
if (f.Exists)
{
string dirName= f.DirectoryName;
}