【问题标题】:Program launching incorrectly when using a shortcut使用快捷方式时程序启动不正确
【发布时间】:2020-05-09 11:38:22
【问题描述】:

当我尝试使用快捷方式或 Process.Start 启动我的程序时,它正在运行我的 if,说我的文件丢失并且它没有启动 WPF 窗口。下图是我试图通过快捷方式启动的/process.start

if (!File.Exists(Environment.CurrentDirectory + "\\Multimanager.dll") && !File.Exists(Environment.CurrentDirectory + "\\Multimanager.dll"))
{
    MessageBox.Show("You are missing the 'Multimanager.dll' and the 'Updater.dll'. Please re-run the MMInstaller.exe to re-download these files", "Missing files", MessageBoxButton.OK, MessageBoxImage.Warning);
    Application.Current.Shutdown();
}
else if (!File.Exists(Environment.CurrentDirectory + "\\Multimanager.dll"))
{
    MessageBox.Show("You are missing the 'Multimanager.dll'. Please re-run the MMInstaller.exe to re-download these files", "Missing files", MessageBoxButton.OK, MessageBoxImage.Warning);
    Application.Current.Shutdown();
}
else if (!File.Exists(Environment.CurrentDirectory + "\\Updater.dll"))
{
    MessageBox.Show("You are missing the 'Updater.dll'. Please re-run the MMInstaller.exe to re-download these files", "Missing files", MessageBoxButton.OK, MessageBoxImage.Warning);
    Application.Current.Shutdown();
}
else
{
    string MMV = Multimanager.MMShared.version().ToString();
    string UV = Updater.UShared.version().ToString();

    var u = new Updater.UpdaterWPF(MMV);
    u.ShowDialog();

    var mm = new Multimanager.UUpdater(UV);
    mm.Show();
    u.Close();
}

编辑:如果我使用 .exe 手动启动程序,它会正常打开(哦,如果它是 64 位应用程序,IDK 与它有任何关系)

编辑:这是我的 process.start Process.Start(path.Text + "\\Multimanager Launcher.exe"); 并且 path.text 是屏幕截图中的目录。 快捷方式是使用本网站上的代码here

【问题讨论】:

  • 请不要将代码发布为图片
  • 看不到您的 process.start 代码会很困难,但是您是否将工作目录设置为程序所在的目录?顺便说一句:请发布您的代码,而不是屏幕截图。
  • 我马上贴出来
  • 1.您的第一个 IF 检查相同的条件 2.尝试使用 appDomain.CurrentDomain.BaseDirectory 而不是 Environment.CurrentDirectory 3.将您的代码发布为格式化文本而不是图片

标签: c# if-statement shortcut system.diagnostics process.start


【解决方案1】:

不要使用Environment.CurrentDirectory 来处理 DLL。这是一个可怕的安全漏洞。您的应用程序正在当前目录而不是应用程序目录中搜索 DLL。希望不是从当前目录加载它们,但这是需要非常小心的事情。

要获取应用程序目录,您可以改用AppDomain.CurrentDomain.BaseDirectory。这不会根据(用户可配置!)当前目录而改变。

【讨论】:

  • AppDomain.CurrentDomain.Directory 似乎工作,我不再收到丢失文件的消息,但 WPF 窗口没有打开,我打开应用程序,没有任何反应。
  • @TristanRead 您的应用程序可能也依赖于其他地方的当前目录。你只需要寻找它。当然,您始终可以将快捷方式的“开始于”目录设置为任何有意义的值。从用户的角度来看,当前目录应该是有用的——例如打开文件对话框的默认起始文件夹。应用程序目录应该与您的应用程序的需求相关 - 只读,包含 DLL、配置文件、资源等。
  • 这有效,感谢您指出快捷方式的“开始”位,在快捷方式中设置shortcut.WorkingDirectory(开始)有效,应用程序现在打开;)
  • @TristanRead 对你有好处 :) 但你仍然应该调查原因。对于应用程序本身需要的任何东西(而不是用户需要的东西),不应使用当前目录。尤其是如果它涉及到加载可执行代码、配置、密码等 - 更改当前目录是每个用户都可以做的事情,所以你需要小心依赖于当前目录的内容。
  • 是的,执行,如果我得到一些有效的代码我总是想了解为什么它有效,为什么其他方法没有,所以我可以将它用于其他事情
猜你喜欢
  • 2014-09-06
  • 2016-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-14
  • 1970-01-01
  • 2011-03-26
  • 2019-03-05
相关资源
最近更新 更多