What is the simplest way to get the directory that a file is in? I'm using this to set a working directory.
获取文件所在目录的最简单方法是什么?我用它来设置一个工作目录。
string filename = @"C:\MyDirectory\MyFile.bat";
In this example, I should get "C:\MyDirectory".
在这个例子中,我应该得到“C:\MyDirectory”。
11 个解决方案
#1
672
If you've definitely got an absolute path, use Path.GetDirectoryName(path)
.
如果您确实有一个绝对路径,请使用path . getdirectoryname (path)。
If you might only get a relative name, use new FileInfo(path).Directory.FullName
.
如果您可能只获得一个相对名称,请使用新的FileInfo(path). directory.fullname。
Note that Path
and FileInfo
are both found in the namespace System.IO
.
注意,Path和FileInfo都可以在名称空间System.IO中找到。
#2
44
System.IO.Path.GetDirectoryName(filename)
#3
19
Path.GetDirectoryName(filename);
#4
12
You can use System.IO.Path.GetDirectory(filename)
, or turn the path into a FileInfo
, and use FileInfo.Directory
.
您可以使用System.IO.Path.GetDirectory(文件名),或者将路径转换为FileInfo,并使用FileInfo. directory。
If you're doing other things with the path, the FileInfo
may have advantages.
如果您正在使用path进行其他操作,那么FileInfo可能具有优势。
#5
9
You can use Path.GetDirectoryName
and just pass in the filename.
您可以使用路径。GetDirectoryName并传入文件名。
MSDN链接
#6
6
Use below mentioned code to get the folder path
使用下面提到的代码获取文件夹路径
Path.GetDirectoryName(filename);
This will return "C:\MyDirectory" in your case
这将返回“C:\MyDirectory”
#7
4
If you are working with a FileInfo
object, then there is an easy way to extract a string
representation of the directory's full path via the DirectoryName
property.
如果您使用的是FileInfo对象,那么有一种简单的方法可以通过DirectoryName属性提取目录完整路径的字符串表示形式。
Description of the FileInfo.DirectoryName
Property via MSDN:
FileInfo的描述。通过MSDN DirectoryName属性:
Gets a string representing the directory's full path.
获取表示目录完整路径的字符串。
Sample usage:
示例用法:
string filename = @"C:\MyDirectory\MyFile.bat";
FileInfo fileInfo = new FileInfo(filename);
string directoryFullPath = fileInfo.DirectoryName; // contains "C:\MyDirectory"
Link to the MSDN documentation.
链接到MSDN文档。
#8
4
You can get the current Application Path using:
您可以使用:
string AssemblyPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location).ToString();
Good Luck!
好运!
#9
1
First, you have to use System.IO namespace. Then;
首先,你必须使用系统。IO命名空间。然后;
string filename = @"C:\MyDirectory\MyFile.bat";
string newPath = Path.GetFullPath(fileName);
or
或
string newPath = Path.GetFullPath(openFileDialog1.FileName));
#10
0
You can use Path.GetFullPath
for most of the case. But if you want to get the path also in the case of the file name is relatively located then you can use the below generic method:
您可以使用路径。GetFullPath用于大多数情况。但是,如果您想获得路径,在文件名相对位于的情况下,那么您可以使用以下通用方法:
string GetPath(string filePath)
{
return Path.GetDirectoryName(Path.GetFullPath(filePath))
}
For example:
例如:
GetPath("C:\Temp\Filename.txt")
return "C:\Temp\"
GetPath(" C:\ Temp \ Filename.txt ")返回" C:\ Temp \ "
GetPath("Filename.txt")
return current working directory
like "C:\Temp\"
GetPath(“Filename.txt”)返回当前工作目录,如“C:\Temp\”
#11
0
Just incase someone else needs it, what I used for my relative path was:
以防别人需要它,我在相对路径中使用的是:
string rootPath = "MyRootDir/MyFolder1/MyFolder2/myFile.pdf";
while (!string.IsNullOrWhiteSpace(Path.GetDirectoryName(rootPath)))
{
rootPath = Path.GetDirectoryName(rootPath);
}
Console.WriteLine(rootPath); //Will print: "MyRootDir"
#1
672
If you've definitely got an absolute path, use Path.GetDirectoryName(path)
.
如果您确实有一个绝对路径,请使用path . getdirectoryname (path)。
If you might only get a relative name, use new FileInfo(path).Directory.FullName
.
如果您可能只获得一个相对名称,请使用新的FileInfo(path). directory.fullname。
Note that Path
and FileInfo
are both found in the namespace System.IO
.
注意,Path和FileInfo都可以在名称空间System.IO中找到。
#2
44
System.IO.Path.GetDirectoryName(filename)
#3
19
Path.GetDirectoryName(filename);
#4
12
You can use System.IO.Path.GetDirectory(filename)
, or turn the path into a FileInfo
, and use FileInfo.Directory
.
您可以使用System.IO.Path.GetDirectory(文件名),或者将路径转换为FileInfo,并使用FileInfo. directory。
If you're doing other things with the path, the FileInfo
may have advantages.
如果您正在使用path进行其他操作,那么FileInfo可能具有优势。
#5
9
You can use Path.GetDirectoryName
and just pass in the filename.
您可以使用路径。GetDirectoryName并传入文件名。
MSDN链接
#6
6
Use below mentioned code to get the folder path
使用下面提到的代码获取文件夹路径
Path.GetDirectoryName(filename);
This will return "C:\MyDirectory" in your case
这将返回“C:\MyDirectory”
#7
4
If you are working with a FileInfo
object, then there is an easy way to extract a string
representation of the directory's full path via the DirectoryName
property.
如果您使用的是FileInfo对象,那么有一种简单的方法可以通过DirectoryName属性提取目录完整路径的字符串表示形式。
Description of the FileInfo.DirectoryName
Property via MSDN:
FileInfo的描述。通过MSDN DirectoryName属性:
Gets a string representing the directory's full path.
获取表示目录完整路径的字符串。
Sample usage:
示例用法:
string filename = @"C:\MyDirectory\MyFile.bat";
FileInfo fileInfo = new FileInfo(filename);
string directoryFullPath = fileInfo.DirectoryName; // contains "C:\MyDirectory"
Link to the MSDN documentation.
链接到MSDN文档。
#8
4
You can get the current Application Path using:
您可以使用:
string AssemblyPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location).ToString();
Good Luck!
好运!
#9
1
First, you have to use System.IO namespace. Then;
首先,你必须使用系统。IO命名空间。然后;
string filename = @"C:\MyDirectory\MyFile.bat";
string newPath = Path.GetFullPath(fileName);
or
或
string newPath = Path.GetFullPath(openFileDialog1.FileName));
#10
0
You can use Path.GetFullPath
for most of the case. But if you want to get the path also in the case of the file name is relatively located then you can use the below generic method:
您可以使用路径。GetFullPath用于大多数情况。但是,如果您想获得路径,在文件名相对位于的情况下,那么您可以使用以下通用方法:
string GetPath(string filePath)
{
return Path.GetDirectoryName(Path.GetFullPath(filePath))
}
For example:
例如:
GetPath("C:\Temp\Filename.txt")
return "C:\Temp\"
GetPath(" C:\ Temp \ Filename.txt ")返回" C:\ Temp \ "
GetPath("Filename.txt")
return current working directory
like "C:\Temp\"
GetPath(“Filename.txt”)返回当前工作目录,如“C:\Temp\”
#11
0
Just incase someone else needs it, what I used for my relative path was:
以防别人需要它,我在相对路径中使用的是:
string rootPath = "MyRootDir/MyFolder1/MyFolder2/myFile.pdf";
while (!string.IsNullOrWhiteSpace(Path.GetDirectoryName(rootPath)))
{
rootPath = Path.GetDirectoryName(rootPath);
}
Console.WriteLine(rootPath); //Will print: "MyRootDir"