而不是运行其路径硬编码的外部程序,我想获得当前的项目目录。我正在使用自定义任务中的进程调用外部程序。
我该怎么做呢?AppDomain.CurrentDomain.BaseDirectory只是给了我VS 2008的位置。
而不是运行其路径硬编码的外部程序,我想获得当前的项目目录。我正在使用自定义任务中的进程调用外部程序。
我该怎么做呢?AppDomain.CurrentDomain.BaseDirectory只是给了我VS 2008的位置。
当前回答
最好的解决方案
string PjFolder1 =
Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).
Parent.Parent.FullName;
其他解决方案
string pjFolder2 = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)));
测试它,AppDomain.CurrentDomain.BaseDirectory在过去的项目中为我工作,现在我得到调试文件夹....所选的好答案就是不行!
//Project DEBUG folder, but STILL PROJECT FOLDER
string pjDebugFolder = AppDomain.CurrentDomain.BaseDirectory;
//Visual studio folder, NOT PROJECT FOLDER
//This solutions just not work
string vsFolder = Directory.GetCurrentDirectory();
string vsFolder2 = Environment.CurrentDirectory;
string vsFolder3 = Path.GetFullPath(".\\");
//Current PROJECT FOLDER
string ProjectFolder =
//Get Debug Folder object from BaseDirectory ( the same with end slash)
Directory.GetParent(pjDebugFolder).
Parent.//Bin Folder object
Parent. //Project Folder object
FullName;//Project Folder complete path
其他回答
string projPath = Path.GetFullPath(@"..\..\..\");
Console.WriteLine(projPath);
这对我来说一直都很有效。试一试。
我没有看到使用字符串的解决方案。连接和字符串。Split + SkipLast 4个元素,在这里。
string projectDir =
string.Join('/', AppDomain.CurrentDomain.BaseDirectory
.Split(new char[] { '/' })
.SkipLast(4));
如果项目在IIS express上运行,则环境。CurrentDirectory可以指向IIS Express所在的位置(默认路径是C:\Program Files (x86)\IIS Express),而不是指向项目所在的位置。
这可能是各种项目最适合的目录路径。
AppDomain.CurrentDomain.BaseDirectory
这就是MSDN的定义。
获取程序集解析器用于探测程序集的基目录。
这个解决方案很适合我,在开发和测试和PROD服务器上使用ASP。NET MVC5通过c#:
var projectDir = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);
如果你需要项目目录中的项目配置文件使用:
$(ProjectDir)
好吧,2021年,有点晚了…但我在许多项目中发现的所有可能性都让我很恼火:
bin /调试 bin / x86 -调试 bin /调试- net5 0-windows。 ...
来吧……我只需要一行程序(或几乎)来处理测试单元中的一些文件;我需要在所有过去、现在、(可能是未来)的项目中使用它。
因此,如果项目名称与它所在的相对文件夹相同:
使用程序集名称选择项目根文件夹名称; 回去找,直到找到那个名字。
代码示例:
string appName = Assembly.GetExecutingAssembly().GetName().Name;
var dir = new DirectoryInfo(Environment.CurrentDirectory);
while (dir.Name != appName) {
dir = Directory.GetParent(dir.FullName);
}
return dir.FullName;