首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用c#更新windows窗体应用程序(通过局域网)?

如何使用c#更新windows窗体应用程序(通过局域网)?
EN

Stack Overflow用户
提问于 2018-02-18 07:35:34
回答 2查看 211关注 0票数 0

我有一个简单的windows窗体应用程序,我想启用自动更新功能(不使用任何第三方应用程序),所以我将这段代码添加到应用程序的表单加载事件中(该表单有一个名为label1的标签,其文本位于form v1.1中)

代码语言:javascript
复制
XDocument doc=XDocument.Load(@"\\PC-10\TEST Update\share\checkver.xml");
double chkver=Convert.ToDouble(doc.Descendants("version").First().Value);
double currver=Convert.ToDouble(label1.Text.Substring(1,3));
if (chkver>currver) {
    string exePath = Application.ExecutablePath;
    string x=exePath.Substring(0,exePath.Length-10)+"\\updater.exe";
    Process.Start(x);
    this.Close();
    }

checkver.xml是一个xml文件,位于与更新的myapp.exe相同的文件夹上,updater.exe位于实际运行的应用程序myapp.exe的文件夹中,即程序文件。

checkver.xml

代码语言:javascript
复制
<?xml version="1.0"?>
<myapp>
    <version>1.2</version>
</myapp>

updater.exe

代码语言:javascript
复制
Console.WriteLine("Updating the app...please wait!");
string exePath = System.Reflection.Assembly.GetEntryAssembly().Location;
string x=exePath.Substring(0,exePath.Length-12);
File.Copy(@"\\PC-10\TEST Update\share\myapp.exe", x+"\\myapp.exe",true);
Console.WriteLine("Update completed!");
Process.Start(x+"\\myapp.exe");

当我从实际的projects bin文件夹运行这个文件时,它运行得很好,但是当我创建一个安装文件并在Program上安装它时,我会得到一个错误

代码语言:javascript
复制
System.UnauthorizedAccessException: Access to the path ...

消息更新程序已经停止运行。为什么会发生这种情况?

还有什么方法可以让这个过程更快(特别是更新检查部分)?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-02-18 08:30:11

我要做的第一件事是检查myapp.exe是否以提升的权限运行:

代码语言:javascript
复制
private static bool IsAdministrator()
{
    var identity = WindowsIdentity.GetCurrent();
    var principal = new WindowsPrincipal(identity);
    return principal.IsInRole(WindowsBuiltInRole.Administrator);
}

运行updater.exe

代码语言:javascript
复制
if(IsAdministrator())
{
    //.......
    if (chkver>currver) 
    {
        string exePath = Application.ExecutablePath;
        string x=exePath.Substring(0,exePath.Length-10)+"\\updater.exe";
        ProcessStartInfo info = new ProcessStartInfo(x); 
        info.UseShellExecute = true; 
        info.Verb = "runas"; 
        Process.Start(info);
        Environment.Exit(1); //Use .Exit(1) instead of this.Close();
    }
}
票数 2
EN

Stack Overflow用户

发布于 2018-02-18 08:30:02

我只是在我的项目中添加了一个app.manifest文件,如下所示

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
  <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <!-- UAC Manifest Options
             If you want to change the Windows User Account Control level replace the 
             requestedExecutionLevel node with one of the following.

        <requestedExecutionLevel  level="asInvoker" uiAccess="false" />
        <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
        <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />

            Specifying requestedExecutionLevel element will disable file and registry virtualization. 
            Remove this element if your application requires this virtualization for backwards
            compatibility.
        -->
        <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
      </requestedPrivileges>
    </security>
  </trustInfo>

  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
      <!-- A list of the Windows versions that this application has been tested on and is
           is designed to work with. Uncomment the appropriate elements and Windows will 
           automatically selected the most compatible environment. -->

      <!-- Windows Vista -->
      <!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />-->

      <!-- Windows 7 -->
      <!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />-->

      <!-- Windows 8 -->
      <!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />-->

      <!-- Windows 8.1 -->
      <!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />-->

      <!-- Windows 10 -->
      <!--<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />-->

    </application>
  </compatibility>

  <!-- Indicates that the application is DPI-aware and will not be automatically scaled by Windows at higher
       DPIs. Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need 
       to opt in. Windows Forms applications targeting .NET Framework 4.6 that opt into this setting, should 
       also set the 'EnableWindowsFormsHighDpiAutoResizing' setting to 'true' in their app.config. -->
  <!--
  <application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
      <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
    </windowsSettings>
  </application>
  -->

  <!-- Enable themes for Windows common controls and dialogs (Windows XP and later) -->
  <!--
  <dependency>
    <dependentAssembly>
      <assemblyIdentity
          type="win32"
          name="Microsoft.Windows.Common-Controls"
          version="6.0.0.0"
          processorArchitecture="*"
          publicKeyToken="6595b64144ccf1df"
          language="*"
        />
    </dependentAssembly>
  </dependency>
  -->

</assembly>

它似乎解决了问题,我had...but不能百分之百确定它是否是正确的解决办法.

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48849461

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档