2

我有一个项目,使用 Visual Web Developer 2008 构建。我希望能够提取(使用 C#)项目现在所在的当前文件夹。直到现在我不得不硬编码真正的路径。

这个项目托管在我的电脑上。

例如,我要生成的路径:

C:\Users\Guy\Desktop\Project

谢谢!盖伊

4

5 回答 5

11

基础程序集解析器目​​录:

AppDomain.CurrentDomain.BaseDirectory;
于 2012-05-23T11:28:36.367 回答
2

最安全的方法:

string temp = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
于 2012-05-23T11:28:12.043 回答
2

如果你只是阅读,那么

Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);

作品。但是,如果您想写信,那么您必须使用:

AppDomain.CurrentDomain.BaseDirectory;

或者你会得到一个

{"URI formats are not supported."}

例外,因为您的路径是

file:\\C:\\Projects\

代替

C:\\Projects
于 2013-10-21T02:13:53.503 回答
1

要将页面(或虚拟目录)映射到文件系统位置(在运行时),您可以使用Server.MapPath("/")

于 2012-05-23T11:29:18.753 回答
1

采用:

Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
于 2012-05-23T11:30:38.800 回答