如何获取命令提示符中的文件夹路径待命,而不是在C#控制台应用程序中?

发布于 2025-01-19 02:39:52 字数 509 浏览 2 评论 0原文

我创建了一个C#控制台应用程序,以便在命令提示符中使用。

这是我的代码:

static void Main(string[] args)
{
    string path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
    Console.WriteLine(path);
}

我的结果:

我想获得 c:\ users \ sophairk 而不是 c:\ testswc

如何修复我的代码以作为我想要的?

提前致谢!

I have created a C# Console App for use in Command Prompt.

Here is my code:

static void Main(string[] args)
{
    string path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
    Console.WriteLine(path);
}

My result:
enter image description here

I want to get C:\Users\Sophairk instead of C:\testswc.

How to fix my code to get as what I want?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懒猫 2025-01-26 02:39:52

当您调用assembly.getEntryAssembly()。位置时,它将返回执行.exe文件的路径。

假设Sophairk是用户的用户名,那么您可以执行此操作:

 string path = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

如果是您想要的当前目录,则可以执行此操作,

string path = Directory.GetCurrentDirectory();

您可以在此处查看文档:
https:/ com/en-us/dotnet/api/system.environment.specialfolder?view = net-6.0

查看还有哪些其他“特殊文件夹”。

When you call Assembly.GetEntryAssembly().Location then it will return the path of the executing .exe file.

Assuming that Sophairk is the username of your user, then you can do this:

 string path = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

If it is the current directory that you want, then you can do this

string path = Directory.GetCurrentDirectory();

You can check out the documentation here:
https://learn.microsoft.com/en-us/dotnet/api/system.environment.specialfolder?view=net-6.0

To see what other "Special Folders" are available.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文