c# - 如何获取应用程序快捷方式的当前目录路径

标签 c# console-application

我想获取当前目录路径,但不是应用程序位置,而是它的快捷方式位置。

我尝试了这些,但它们返回了应用程序的位置。

Directory.GetCurrentDirectory();
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);

最佳答案

根据 MSDN 中的进程 API 引用,给定进程的进程 STARTUPINFO 结构在 title 成员中包含有关快捷方式 .lnk 文件的信息。在这种情况下设置的 dwFlags 结构成员中有一个标志 - 所以看起来这并不总是设置(我猜你是否直接运行了 exe)

来自 MSDN:

STARTF_TITLEISLINKNAME: 0x00000800

The lpTitle member contains the path of the shortcut file (.lnk) that the user invoked to start this process. This is typically set by the shell when a .lnk file pointing to the launched application is invoked. Most applications will not need to set this value. This flag cannot be used with STARTF_TITLEISAPPID.

Reference here .

关于c# - 如何获取应用程序快捷方式的当前目录路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15758941/

相关文章:

c# - 如何在 Windows 应用程序中集成命令提示符/终端?

windows - 使用 "cmd"和 "start"命令获取应用程序的退出代码

java - 没有容器的jpa

c# - TCPClient没有网卡能用吗?

c++ - 如何在控制台应用程序中使用仅消息窗口接收消息?

c - c中的简单菜单

c# 在对象上绘制文本

c# - 如何使用带有 Npoi 的数据集创建 Excel 电子表格

c# - T4文本模板: method to return html expression

c# - 将 MySQL 列与方法中的参数变量进行比较