我正在发布一个Windows项目,然后单击表单,我正在安装另一个安装程序进行安装。
我没有在按钮上的点击事件上获得当前的应用程序启动路径。
在调试和发布时,它显示了正确的路径,但在发布后它给出了
C:\用户\用户名\应用数据\本地\应用\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:\用户\用户名\应用数据\本地\应用\2.0 路径
您正在获得该路径,因为它是ClickOnce使用的路径。ClickOnce 应用程序安装在安装它们的用户的配置文件下。
编辑:
方法一:
这是一种获取安装应用程序的路径的方法(仅在安装了应用程序时才有效)(其中部分内容由@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(@"SoftwareMicrosoftWindowsCurrentVersionUninstall"))
{
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
顺便说一句,是ClickOnce部署吗?如果是这样,那么您得到的目录看起来是正确的。