申请管理员权限以编辑系统文件

申请管理员权限以编辑系统文件

本文介绍了申请管理员权限以编辑系统文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#的新手,我正在Visual Studio中开发Windows窗体应用程序.该应用程序使用 ICSharpCode.SharpZipLib 库来更新zip的内容.

New to C#, I'm developing a windows form application inside visual studio. The app uses ICSharpCode.SharpZipLib library to update a zip's content.

当zip文件位于非系统分区内时,例如说D:\myzip.zip,文件将正确更新.但是,如果zip位于系统分区中,则C:\myzip.zip应用程序将返回错误;否则,该应用程序将返回错误. Could not find file C:\myzip.zip即使文件在那里!

When the zip file is inside a non system partition, say like D:\myzip.zip the files gets updated correctly. But, if the zip is in the system partition, C:\myzip.zip the app returns an error; Could not find file C:\myzip.zip even though the file is there!

这与管理员权限有关,因为当我以管理员身份运行该应用程序时,它可以工作.

This has to do with admin privileges because when I run the app as administrator it works.

问题是;默认情况下如何启用这些特权?

The question is; how could I enable those privileges by default?

推荐答案

您可以添加应用清单以使您的应用准确地以管理员身份运行( msdn ).

You can add app manifest to exact your app to run as administrator (msdn).

将以下代码添加到清单文件中:

Add the following code to your manifest file:

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

逐步添加应用清单:

  1. 在解决方案资源管理器中右键单击该项目.
  2. 点击添加新项.
  3. 找到并选择应用程序清单文件:
  1. Right-click on the project in the Solution Explorer.
  2. Click Add New Item.
  3. Find and select Application Manifest File:

编辑应用清单文件:

 <?xml version="1.0" encoding="utf-8"?>
    <asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <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">
            <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
          </requestedPrivileges>
        </security>
      </trustInfo>

      <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
        <application>
        </application>
      </compatibility>
    </asmv1:assembly>

这篇关于申请管理员权限以编辑系统文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 12:55