-1

在我的 Web 项目解决方案中,我添加了许多项目。

我在其中一个项目中将文件夹名称命名为“本地化”。

因此,通过在另一个项目后面的代码中指定文件夹名称,我如何获取文件夹的完整路径。

如果文件夹存在于其中一个项目中,那么我需要获取完整路径。

4

2 回答 2

1

您提到“如果文件夹存在”,因此您需要先测试该文件夹是否存在:

DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/FolderName"));
if (dir.Exists)
{
    //dir.FullName will get you the path.
    //This is the same things as Server.MapPath("~/FolderName"), but that
    //will return a path even if the folder isn't there.
}
else
{
    //folder doesn't exist.
}

由于您试图走出您的项目,您可以执行以下操作(取决于您的解决方案结构):

string path = Path.GetFullPath(Path.Combine(Server.MapPath("~"), @"..\OtherProjectName/FolderName")); 
DirectoryInfo dir = new DirectoryInfo(path);
于 2013-09-25T18:04:31.530 回答
1

您可以使用此示例获取另一个文件夹的完整路径。

System.IO.Path.GetFullPath(System.IO.Path.Combine(Server.MapPath("~"), "../Localization/"));
于 2013-09-25T18:54:14.947 回答