我试图使用下面的代码获取控制台应用程序的目录,
Assembly.GetExecutingAssembly().Location
Run Code Online (Sandbox Code Playgroud)
但是这个给了我汇编所在的位置.这可能与我执行应用程序的位置不同.
我的控制台应用程序解析没有参数的日志.它必须转到logs/可执行文件夹中的文件夹,或者如果我给它一个路径来logs/解析它.
Ste*_*fan 90
使用Environment.CurrentDirectory.
获取或设置当前工作目录的标准路径.
(MSDN Environment.CurrentDirectory属性)
string logsDirectory = Path.Combine(Environment.CurrentDirectory, "logs");
Run Code Online (Sandbox Code Playgroud)
如果您的应用程序在c:\ Foo\Bar中 运行,logsDirectory则将指向 c:\ Foo\Bar\logs.
Sun*_*nny 29
用这个 :
System.Reflection.Assembly.GetExecutingAssembly().Location
Run Code Online (Sandbox Code Playgroud)
结合它
System.IO.Path.GetDirectoryName if all you want is the directory.
Run Code Online (Sandbox Code Playgroud)
Mic*_*ida 17
Scott Hanselman 发表了一篇博文,内容如下:
using System;
using System.Diagnostics;
using System.IO;
namespace testdir
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine($"Launched from {Environment.CurrentDirectory}");
Console.WriteLine($"Physical location {AppDomain.CurrentDomain.BaseDirectory}");
Console.WriteLine($"AppContext.BaseDir {AppContext.BaseDirectory}");
Console.WriteLine($"Runtime Call {Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName)}");
}
}
Run Code Online (Sandbox Code Playgroud)
他在 .NET Core 3.1 中使用它,但是,我正在运行 .NET 4.8 并且它对我有用。
最安全的方式:
string temp = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
81536 次 |
| 最近记录: |