繁体   English   中英

我如何获取当前路径而不是 c# 中的 exe 路径?

[英]How can iI get the current path and not the path of exe in c#?

我在C#中发布了一个console project ,并将其放在path environment FE c:\some\path\project.exe中。 可执行文件具有以下代码。

Console.WriteLine(Environment.GetCommandLineArgs()[0]);
Console.WriteLine(System.AppContext.BaseDirectory);
Console.WriteLine(System.AppDomain.CurrentDomain.BaseDirectory);
Console.WriteLine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location));
Console.WriteLine(Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName));

当我执行它时。 在 cmd 我得到以下内容:

c:\here\here\here > project.exe

c:\some\path\project.dll
c:\some\path\
c:\some\path\
c:\some\path
c:\some\path

我需要获取路径c:\here\here\here

试试这个: Console.WriteLine(Directory.GetCurrentDirectory());

除了 Dharati 的回答中建议的内容之外,您还可以执行以下操作:

string fullPath = System.Reflection.Assembly.GetEntryAssembly().Location;
string noFileNamePath = fullPath.Substring(0, (fullPath.LastIndexOf('\\') + 1));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM