[英]Not able to get Application startup path on click on setup.exe
我正在发布一个Windows项目,点击表单我正在安装另一个安装程序。
我没有在clickevent按钮上获取当前的应用程序启动路径。
在调试和发布时,它显示正确的路径,但在发布后它给出了
C:\\ Users \\ username \\ AppData \\ Local \\ Apps \\ 2.0路径
我已经使用过:
Application.StartupPath
Application.Executablepath
Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location))
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase))
Path.Combine(Directory.GetCurrentDirectory())
但它总是显示没用
C:\\ Users \\ username \\ AppData \\ Local \\ Apps \\ 2.0路径
您正在获得该路径,因为它是ClickOnce使用的路径。 ClickOnce应用程序安装在安装它们的用户的配置文件下。
编辑:
方法1:
这是一种获取安装应用程序的路径的方法(仅在安装了应用程序时才有效)(部分内容由@codeConcussion编写):
// productName is name you assigned to your app in the
// Project properties -> Publish -> Publish Settings
public static string GetInstalledFromDir(string productName)
{
using (var key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall"))
{
if (key != null)
{
var appKey = key.GetSubKeyNames().FirstOrDefault(x => GetValue(key, x, "DisplayName") == productName);
return appKey == null ? null : GetValue(key, appKey, "UrlUpdateInfo");
}
}
return null;
}
private static string GetValue(RegistryKey key, string app, string value)
{
using (var subKey = key.OpenSubKey(app))
{
if (subKey == null || !subKey.GetValueNames().Contains(value))
{
return null;
}
return subKey.GetValue(value).ToString();
}
}
以下是如何使用它:
Uri uri = new Uri(GetInstalledFromDir("ProductName"));
MessageBox.Show(Path.GetDirectoryName(HttpUtility.UrlDecode(uri.AbsolutePath)));
方法2:
你也可以试试
System.Deployment.Application.ApplicationDeployment.CurrentDeployment.ActivationUri
但我认为只有当您的应用程序是从互联网安装时才有效
尝试这个:
Process.GetCurrentProcess().MainModule.FileName
BTW,是ClickOnce部署吗? 如果是这样,那么你得到的目录看起来是正确的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.