2013-07-18 67 views
1

我在我的机器的E驱动器中创建了一个目录名称'DbInventory' 现在在我的c#应用程序中,我想获取此directoty(DbInventory)的完整路径。如何获取目录的完整路径

波纹管我提的代码我用

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

但这currentDirectoryName字符串在C盘恢复文件的位置。

+1

你有没有使用Google? –

+1

为什么您确定应用程序将在E驱动器上查找目录而不是C驱动器上的目录? –

+0

在猜测...... C盘位置是否包含“bin/debug”? – Sayse

回答

3

首先,DirectoryInfo构造函数接受参数与完整路径的字符串。当您只写一个文件夹名称时,它会得到您的Visual Studio默认路径的位置,这在您的C:/驱动器和Bin/Debug文件夹中最为常见。

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

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

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

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

0

尝试使用Server.Mappath

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

希望这会为你工作

1

你正在创建与

new DirectoryInfo("DbInventory"); 

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

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

+0

我同意你,但如果OP有完整的路径,那么OP将不需要使用目录信息:) +1 – Sayse

1

您可以使用Path.GetDirectoryName

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