简体   繁体   中英

Access is denied when deleting/downloading a new .exe in VB.net

I know there are a lot of similar questions, but they seem to be about AppData. However, my path is 'C:\\Program Files (x86)\\Project\\My Product Name\\Database\\Project.exe'. I get an error message when trying to delete this saying System.UnauthorizedAccessException: Access to the path 'C:\\Program Files (x86)\\Project\\My Product Name\\Database\\Project.exe' is denied. Is this because you have to have Admin Privileges for changing things in Program Files? If so, could I create it so it installs it into Libraries/Documents or something? Extra info if needed -

I am using Install Shield and Visual Studio 2013.

Update-----------------------------------------------------

The way I am trying to delete it/replace the file is:

I have "Main Form" and "Updater Form". And what happens is, MainForm opens UpdaterForm and then closes itself. Like so...

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

Then in the UpdaterForm this happens...

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

However on My.Computer.FileSystem.DeleteFile(Application.StartupPath & "/Project.exe") it says I do not have the permission to edit the file path. I think it is is just the case of moving it somewhere else or making it so UpdaterForm has permissions.

Also, what I was wondering is would it be better for the UpdaterForm to be a different .exe? Or could I just keep it the same?

If you know a lot about Install Shield, could I just use an Upgrade Path, and select to old .msi file? But I am not to sure about how it goes about updating etc. If you know more about it could you please explain?

As I mentioned in my comment: I expect this is a UAC issue.

The method I know of to gain higher privileges is to request a higher execution level in your application manifest file (app.manifest -> Click Show All Files for your project -> expand My Project). In there you will find a section that looks like this:

<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>

You want to change your requestedExecutionLevel to requireAdministrator or highestAvailable .

This article at Code Project seems to have a fairly nice walkthrough. Somebody has asked about elevating a process that is already running on Stack Overflow . It isn't possible, but there is good information here and some useful links.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM