我希望获得当前的Project Dir,而不是运行其路径硬编码的外部程序.我正在使用自定义任务中的进程调用外部程序.
我该怎么办?AppDomain.CurrentDomain.BaseDirectory只是给了我VS 2008的位置.
moh*_*eeh 237
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 directory
string projectDirectory = Directory.GetParent(workingDirectory).Parent.FullName;
Run Code Online (Sandbox Code Playgroud)
Ira*_*tro 105
您可以尝试这两种方法之一.
string startupPath = System.IO.Directory.GetCurrentDirectory();
string startupPath = Environment.CurrentDirectory;
Run Code Online (Sandbox Code Playgroud)
告诉我,哪一个对你好
hin*_*531 24
如果项目Environment.CurrentDirectory在IIS Express 上运行,则可以指向IIS Express所在的位置(默认路径为C:\ Program Files(x86)\ IIS Express),而不是项目所在的位置.
这可能是各种项目最合适的目录路径.
AppDomain.CurrentDomain.BaseDirectory
Run Code Online (Sandbox Code Playgroud)
这是MSDN定义.
获取程序集解析程序用于探测程序集的基目录.
nh4*_*3de 17
这也将通过从当前执行目录导航两个级别来为您提供项目目录(这不会返回每个构建的项目目录,但这是最常见的).
System.IO.Path.GetFullPath(@"..\..\")
Run Code Online (Sandbox Code Playgroud)
当然,您希望在某种验证/错误处理逻辑中包含此内容.
abe*_*406 10
如果您不想知道解决方案所在的目录是什么,则需要执行以下操作:
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*/}
}
Run Code Online (Sandbox Code Playgroud)
如果只使用GetCurrrentDirectory()方法,则无论是调试还是发布,都会获得构建文件夹.我希望这有帮助!如果您忘记了验证,它将是这样的:
var startDirectory = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName;
Run Code Online (Sandbox Code Playgroud)
如果您确实想确保获得源项目目录,无论 bin 输出路径设置为什么:
添加预构建事件命令行(Visual Studio:项目属性 -> 构建事件):
echo $(MSBuildProjectDirectory) > $(MSBuildProjectDirectory)\Resources\ProjectDirectory.txt
将文件添加ProjectDirectory.txt到项目的Resources.resx中(如果尚不存在,右键单击项目 - >添加新项目 - >资源文件)
Resources.ProjectDirectory。这个解决方案对我来说效果很好,在开发以及通过 C#使用ASP.NET MVC5 的TEST 和 PROD 服务器上:
var projectDir = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);
Run Code Online (Sandbox Code Playgroud)
如果您需要项目配置文件中的项目目录,请使用:
$(ProjectDir)
Run Code Online (Sandbox Code Playgroud)
string projPath = Path.GetFullPath(@"..\..\..\");
Console.WriteLine(projPath);
Run Code Online (Sandbox Code Playgroud)
这对我来说一直很有效。搏一搏。
我也在寻找这个.我有一个运行HWC的项目,我想让网站远离应用程序树,但我不想将它保留在调试(或发布)目录中.FWIW,已接受的解决方案(以及此解决方案)仅标识可执行文件所在的目录.
为了找到该目录,我一直在使用
string startupPath = System.IO.Path.GetFullPath(".\\").
Run Code Online (Sandbox Code Playgroud)
小智 5
我也遇到过类似的情况,谷歌无果后,我声明了一个公共字符串,它修改了调试/发布路径的字符串值以获取项目路径。使用此方法的一个好处是,由于它使用当前项目的目录,因此无论您是在调试目录还是发布目录中工作都无关紧要:
public string DirProject()
{
string DirDebug = System.IO.Directory.GetCurrentDirectory();
string DirProject = DirDebug;
for (int counter_slash = 0; counter_slash < 4; counter_slash++)
{
DirProject = DirProject.Substring(0, DirProject.LastIndexOf(@"\"));
}
return DirProject;
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以随时调用它,只需一行:
string MyProjectDir = DirProject();
Run Code Online (Sandbox Code Playgroud)
这在大多数情况下应该有效。
基于Gucu112的答案,但对于.NET Core控制台/窗口应用程序,应为:
string projectDir =
Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"..\..\.."));
Run Code Online (Sandbox Code Playgroud)
我正在xUnit项目的.NET Core窗口应用程序中使用它。
using System;
using System.IO;
// Get the current directory and make it a DirectoryInfo object.
// Do not use Environment.CurrentDirectory, vistual studio
// and visual studio code will return different result:
// Visual studio will return @"projectDir\bin\Release\netcoreapp2.0\", yet
// vs code will return @"projectDir\"
var currentDirectory = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
// On windows, the current directory is the compiled binary sits,
// so string like @"bin\Release\netcoreapp2.0\" will follow the project directory.
// Hense, the project directory is the great grand-father of the current directory.
string projectDirectory = currentDirectory.Parent.Parent.Parent.FullName;
Run Code Online (Sandbox Code Playgroud)
我对迄今为止发布的所有解决方案的随意性感到惊讶。
获取 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 );
}
}
Run Code Online (Sandbox Code Playgroud)
该string?要求的C#与一个漂亮的晚版#nullable enable; 如果你没有它,那么只需删除?.
该Assert()功能是我自己,用自己的替换它。
该函数GetSourceFilePathName()定义如下:
using System.Runtime.CompilerServices
public static string GetSourceFilePathName( [CallerFilePath] string? callerFilePath = null ) //
=> callerFilePath ?? "";
Run Code Online (Sandbox Code Playgroud)
一旦你有了这个,你可以按如下方式使用它:
string projectSourcePath = ProjectSourcePath.Value;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
268558 次 |
| 最近记录: |