而不是运行其路径硬编码的外部程序,我想获得当前的项目目录。我正在使用自定义任务中的进程调用外部程序。
我该怎么做呢?AppDomain.CurrentDomain.BaseDirectory只是给了我VS 2008的位置。
而不是运行其路径硬编码的外部程序,我想获得当前的项目目录。我正在使用自定义任务中的进程调用外部程序。
我该怎么做呢?AppDomain.CurrentDomain.BaseDirectory只是给了我VS 2008的位置。
当前回答
如果你真的想确保你得到源项目目录,不管bin输出路径设置为什么:
添加一个预构建事件命令行(Visual Studio:项目属性->构建事件): echo $(MSBuildProjectDirectory) > $(MSBuildProjectDirectory)\Resources\ProjectDirectory.txt 将ProjectDirectory.txt文件添加到资源。resx项目(如果它还不存在,右键单击项目->添加新项目->资源文件) 使用Resources.ProjectDirectory从代码中访问。
其他回答
获取c#项目根文件夹的正确方法是利用[CallerFilePath]属性获取源文件的完整路径名,然后减去文件名加扩展名,留下项目的路径。
下面是如何做到这一点:
在项目的根文件夹中,添加包含以下内容的ProjectSourcePath.cs文件:
internal static class ProjectSourcePath
{
private const string myRelativePath = nameof(ProjectSourcePath) + ".cs";
private static string? lazyValue;
public static string Value => lazyValue ??= calculatePath();
private static string calculatePath()
{
string pathName = GetSourceFilePathName();
Assert( pathName.EndsWith( myRelativePath, StringComparison.Ordinal ) );
return pathName.Substring( 0, pathName.Length - myRelativePath.Length );
}
}
字符串?需要一个非常晚的c#版本#nullable enable;如果你没有,那就去掉?。
Assert()函数是我自己的;如果你喜欢过危险的生活,你可以用你自己的代替它,或者省略它。
函数GetSourceFilePathName()的定义如下:
using System.Runtime.CompilerServices
public static string GetSourceFilePathName( [CallerFilePath] string? callerFilePath = null ) //
=> callerFilePath ?? "";
一旦你有了以上这些,你可以这样使用它:
string projectSourcePath = ProjectSourcePath.Value;
1 'proper'在:万无一失的;可靠;没有假设;不牢靠的:不牢靠的;不一定在某些项目上成功,但在另一些项目上失败;当你改变不相关的东西时,不太可能毫无征兆地坏掉;等。
最好的解决方案
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
如果你想知道你的解决方案所在的目录是什么,你需要这样做:
var parent = Directory.GetParent(Directory.GetCurrentDirectory()).Parent;
if (parent != null)
{
var directoryInfo = parent.Parent;
string startDirectory = null;
if (directoryInfo != null)
{
startDirectory = directoryInfo.FullName;
}
if (startDirectory != null)
{ /*Do whatever you want "startDirectory" variable*/}
}
如果你只使用getcurrentdirectory()方法,无论你是在调试还是发布,你都会得到构建文件夹。希望这对你有所帮助!如果你忘记了验证,它会是这样的:
var startDirectory = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName;
using System;
using System.IO;
// This will get the current WORKING directory (i.e. \bin\Debug)
string workingDirectory = Environment.CurrentDirectory;
// or: Directory.GetCurrentDirectory() gives the same result
// This will get the current PROJECT bin directory (ie ../bin/)
string projectDirectory = Directory.GetParent(workingDirectory).Parent.FullName;
// This will get the current PROJECT directory
string projectDirectory = Directory.GetParent(workingDirectory).Parent.Parent.FullName;
(因为22个答案是不够的……这里还有一个....)
Mike Nakis给出了一个很好的答案,我在上面添加了一些改进。这只是对他的漂亮代码稍加修饰的版本。
正如Mike指出的,这个类文件必须在项目的根目录中。
下面的内容我没有遇到任何问题,但可能有我没有意识到的细微差别。YMMV。
using System.IO;
using System.Runtime.CompilerServices;
namespace Whatever
{
internal static class ProjectPathInfo
{
public static string CSharpClassFileName = nameof(ProjectPathInfo) + ".cs";
public static string CSharpClassPath;
public static string ProjectPath;
public static string SolutionPath;
static ProjectPathInfo() {
CSharpClassPath = GetSourceFilePathName();
ProjectPath = Directory.GetParent(CSharpClassPath)!.FullName;
SolutionPath = Directory.GetParent(ProjectPath)!.FullName;
}
private static string GetSourceFilePathName( [CallerFilePath] string? callerFilePath = null ) => callerFilePath ?? "";
}
}