在 VB.net 中删除/下载新的 .exe 时访问被拒绝

问题描述 投票:0回答:1

我知道有很多类似的问题,但它们似乎是关于AppData的。但是,我的路径是“C:\Program Files (x86)\Project\My Product Name\Database\Project.exe”。当我尝试删除这句话时收到一条错误消息

System.UnauthorizedAccessException: Access to the path 'C:\Program Files (x86)\Project\My Product Name\Database\Project.exe' is denied.
这是因为您必须拥有管理员权限才能更改程序文件中的内容吗?如果是这样,我可以创建它以便将其安装到库/文档或其他东西中吗?如果需要额外信息 -

我正在使用 Install Shield 和 Visual Studio 2013。

更新------------------------------------------------------------ ------

我尝试删除它/替换文件的方法是:

我有“主表单”和“更新程序表单”。发生的情况是,MainForm 打开 UpdaterForm,然后自行关闭。就像这样...

 Private Sub UpdateBtn_Click(sender As Object, e As EventArgs) Handles UpdateBtn.Click
    Updater.Show()
    Me.Close()
End Sub

然后在 UpdaterForm 中会发生这种情况...

Private Sub UpdateBtn_Click(sender As Object, e As EventArgs) Handles UpdateBtn.Click
    Main_Menu.Close()
    Dim Web As New WebClient
    My.Computer.FileSystem.DeleteFile(Application.StartupPath & "/Project.exe")
    My.Computer.Network.DownloadFile("MYLINK", Application.StartupPath & "/Project.exe")
End Sub

但是在

My.Computer.FileSystem.DeleteFile(Application.StartupPath & "/Project.exe")
上它说我没有编辑文件路径的权限。我认为这只是将其移动到其他地方或使其具有权限的情况。

另外,我想知道 UpdaterForm 是不同的 .exe 会更好吗?或者我可以保持不变吗?

如果您对 Install Shield 了解很多,我可以使用升级路径并选择旧的 .msi 文件吗?但我不确定它是如何进行更新等的。如果您了解更多信息,您能解释一下吗?

vb.net uac unauthorizedaccessexcepti
1个回答
0
投票

正如我在评论中提到的:我希望这是一个 UAC 问题。

我所知道的获得更高权限的方法是在应用程序清单文件中请求更高的执行级别(app.manifest -> 单击项目的显示所有文件 -> 展开我的项目)。在那里你会发现一个看起来像这样的部分:

<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 node will disable file and registry virtualization.
        If you want to utilize File and Registry Virtualization for backward 
        compatibility then delete the requestedExecutionLevel node.
    -->
    <requestedExecutionLevel level="asInvoker" uiAccess="false" />
  </requestedPrivileges>
  <applicationRequestMinimum>
    <PermissionSet Unrestricted="true" ID="Custom" SameSite="site" />
    <defaultAssemblyRequest permissionSetReference="Custom" />
  </applicationRequestMinimum>
</security>

您想要将

requestedExecutionLevel
更改为
requireAdministrator
highestAvailable

Code Project 的这篇文章似乎有一个相当不错的演练。有人询问如何提升已在 Stack Overflow 上运行的进程。这是不可能的,但这里有很好的信息和一些有用的链接。

© www.soinside.com 2019 - 2024. All rights reserved.