简体   繁体   English

如何获取目录的完整路径

[英]How to get the full path of a directory

I have create a directory name 'DbInventory' in E drive of my machine Now in my c# application I want to get the full path of this directoty(DbInventory). 我在我的机器的E驱动器中创建了一个目录名'DbInventory'现在在我的c#应用程序中我想获得这个直接的完整路径(DbInventory)。

bellow I am mentioning the code I have used 吼我提到我用过的代码

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

But this currentDirectoryName string return the file location in C drive. 但是这个currentDirectoryName字符串返回C盘中的文件位置。

First of all, DirectoryInfo constructor takes parameter as a string with full path . 首先, DirectoryInfo构造函数将参数作为具有完整路径的字符串。 When you just write a folder name, it gets the location of your Visual Studio's default path which is most common in your C:/ drive and under Bin/Debug folder. 当您只编写文件夹名称时,它会获取Visual Studio默认路径的位置,这在C:/驱动器和Bin/Debug文件夹下最常见。

So in my computer, your currentDirectoryName will be; 所以在我的电脑中,你的currentDirectoryName将是;

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

If you want to get full path name, you can use Path.GetDirectoryName method; 如果要获取完整路径名,可以使用Path.GetDirectoryName方法;

Returns the directory information for the specified path string. 返回指定路径字符串的目录信息。

You are creating with the 你正在创建

new DirectoryInfo("DbInventory");

a new directory on the default drive (C). 默认驱动器(C)上的新目录。 Take a look at: MSDN 看看: MSDN

If you want to get the directory info object of the already created one on drive E you have to specify the path. 如果要获取驱动器E上已创建的目录信息对象,则必须指定路径。

You can use Path.GetDirectoryName 您可以使用Path.GetDirectoryName

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

try server.mappath 尝试server.mappath

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

hope this will work for you 希望这对你有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM