在另一个项目中按文件夹名称查找完整路径

时间:2013-09-25 17:51:37

标签: c# asp.net path

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

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

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

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

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:1)

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

System.IO.Path.GetFullPath(System.IO.Path.Combine(Server.MapPath("~"), "../Localization/"));
相关问题