简体   繁体   English

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

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

I publish a console project in C# and I put it in the path environment FE c:\some\path\project.exe .我在C#中发布了一个console project ,并将其放在path environment FE c:\some\path\project.exe中。 the excecutable has the following code.可执行文件具有以下代码。

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));

When I execute it.当我执行它时。 in the cmd I Get the following:在 cmd 我得到以下内容:

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

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

I need to get the path c:\here\here\here我需要获取路径c:\here\here\here

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

You could do something like this besides what was suggested in Dharati's answer:除了 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