ClickOnce 的“应用程序标识未设置”

And*_*ndy 2 c# clickonce

我在这个问题上摸不着头脑,并花了很长时间研究它。我基于这个问题查看的每个问题看起来都与调试有关(我也有这个调试问题)。然而,这并不是这背后的原因。

In my application I gave the user the option to create a shortcut so that the application will run automatically in startup (I found this a much easier approach than editing the registry).

string copyPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\";
using (StreamWriter writer = new StreamWriter(copyPath + "\\" + "ZApp.url"))
{
    string app = System.Reflection.Assembly.GetExecutingAssembly().Location;
    writer.WriteLine("[InternetShortcut]");
    writer.WriteLine("URL=file:///" + app);
    writer.WriteLine("IconIndex=0");
    string icon = app.Replace('\\', '/');
    writer.WriteLine("IconFile=" + icon);
    writer.Flush();
}
Run Code Online (Sandbox Code Playgroud)

This code works great. However, whenever the application checks for an update in the background, it throws the Application identity is not set error.

This is the code that I use for checking for an update:

ApplicationDeployment deploy = ApplicationDeployment.CurrentDeployment; //<- Error thrown here
UpdateCheckInfo update = deploy.CheckForDetailedUpdate();
Run Code Online (Sandbox Code Playgroud)

Again, it works great if I start the application using the icon that ClickOnce places on my desktop, however not great if I use the shortcut icon that my code creates in the startup folder. Is there a way for me to programmatically get round this?

Her*_*rdo 5

部署状态是错误的原因。正如rudolf_franek已经解释的那样,该应用程序不是网络部署的。在访问之前应用检查CurrentDeployment将使您的应用程序更安全:

if (ApplicationDeployment.IsNetworkDeployed)
{
    // accessing the CurrentDeployment won't throw an exception
    var deploy = ApplicationDeployment.CurrentDeployment;
}
Run Code Online (Sandbox Code Playgroud)

但是要解决导致ApplicationDeployment.IsNetworkDeployed错误的问题,您应该查看此MSDN 论坛帖子tl; dr:创建引用*.exe文件的快捷方式将使应用程序作为独立运行。

解决方案:
您应该创建一个适当的快捷方式,在其中引用应用程序部署清单(又名*.application文件)。这应该可以解决问题。