【问题标题】:How can I get path of setup.exe from custom action?如何从自定义操作中获取 setup.exe 的路径?
【发布时间】:2016-10-20 14:26:07
【问题描述】:

我的setup.exe在e:\setup.exe中,我试过这段代码:

System.AppDomain.CurrentDomain.BaseDirectory;

返回 c:\Windows\syswow64\

Application.ExecutablePath;

返回 c:\Windows\syswow64\MsiExec.exe

Application.StartupPath

返回 c:\Windows\syswow64\

我需要一些东西返回 e:\

【问题讨论】:

  • 你可以使用Directory.GetCurrentDirectory()获取当前正在运行的进程目录,你也可以使用DriveInfo[] allDrives = DriveInfo.GetDrives();获取所有驱动器,如果你正在寻找的话
  • 问题在于,在您的代码运行时,它作为.msi 文件的一个组件运行,正如大多数迹象告诉您的那样,该文件实际上是由@987654327 执行的@ 程序。如果一种访问此信息的方法,您将寻找特定于访问 MSI 属性的 MSI,而不是查看当前可执行文件/工作目录的通用 .NET 代码。

标签: c# windows installation


【解决方案1】:
using System.IO;
string exeDir = Directory.GetCurrentDirectory();

您也可以通过反射获得exe完整路径。

string exeLocation = System.Reflection.Assembly.GetEntryAssembly().Location;

你也可以。

string exeDir = AppDomain.CurrentDomain.BaseDirectory;
string exeLocation = Assembly.GetEntryAssembly().Location;

另一种方式:

string dir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);

【讨论】:

  • 感谢您的回答,您的一些建议返回安装后应用程序所在的路径,但其中一个返回 setup.exe 的路径
  • 这就是为什么我发布了多个答案,因为问题中没有很好地解释它:)
  • 您可以在 CustomActionData 属性中设置目标路径,也可以获取上下文参数
【解决方案2】:

获取当前工作目录使用

Directory.GetCurrentDirectory();

位于System.IO

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 2013-12-20
    相关资源
    最近更新 更多