如何获取目录的完整路径

本文关键字:路径 何获取 获取 | 更新日期: 2023-09-27 18:23:38

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

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

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

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

如何获取目录的完整路径

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

所以在我的电脑里,你的currentDirectoryName将是;

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

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

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

您正在使用创建

new DirectoryInfo("DbInventory");

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

如果你想获得驱动器E上已经创建的目录信息对象,你必须指定路径。

您可以使用Path.GetDirectoryName

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

尝试server.mappath

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

希望这将为您工作