如何获取目录的完整路径

时间:2013-07-18 06:29:04

标签: c# asp.net file

我在我的机器的E驱动器中创建了一个目录名'DbInventory' 现在在我的c#应用程序中,我想获得这个直接(DbInventory)的完整路径。

下面我提到我使用过的代码

DirectoryInfo info = new DirectoryInfo("DbInventory");
string currentDirectoryName = info.FullName;

但是这个currentDirectoryName字符串返回C盘中的文件位置。

4 个答案:

答案 0 :(得分:3)

首先,DirectoryInfo构造函数将参数作为带有完整路径的字符串。当您只编写文件夹名称时,它会获取Visual Studio默认路径的位置,该路径在C:/驱动器和Bin/Debug文件夹下最常见。

因此,在我的电脑中,您的currentDirectoryName将会是

C:\Users\N53458\Documents\Visual Studio 2008\Projects\1\1\bin\Debug\DbInventory

如果您想获得完整路径名,可以使用Path.GetDirectoryName方法;

  

返回指定路径字符串的目录信息。

答案 1 :(得分:1)

您正在使用

创建
new DirectoryInfo("DbInventory");

默认驱动器(C)上的新目录。看一眼: MSDN

如果要获取驱动器E上已创建的目录信息对象,则必须指定路径。

答案 2 :(得分:1)

您可以使用Path.GetDirectoryName

DirectoryInfo info = new DirectoryInfo("DbInventory");
string currentDirectoryName = info.FullName;
string directoryPath = Path.GetDirectoryName(path);

答案 3 :(得分:0)

尝试server.mappath

string currentDirectoryName =Server.MapPath(info.FullName);

希望这对你有用

相关问题