1

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?

  • your app very likely will not have rights to write to Program files. Moreover, if Project.EXE is doing the downloading, you cant replace a file while it is in use – Ňɏssa Pøngjǣrdenlarp Aug 25 '14 at 20:15
  • This looks like a common UAC issue. Basically your application needs to request elevated status before attempting to update files in a protected directory (such asn `Program Files (x86)`). – JoelC Aug 25 '14 at 20:18
  • @JoelC Oh right, well how would I go about doing that, would it be difficult? –  Aug 25 '14 at 20:35
  • @Plutonix It is fine, it will not interfere. –  Aug 25 '14 at 20:36
  • Can you give us more information on how you are trying to delete `Program.exe` please? Are you deleting it from another application? Thanks. – JoelC Aug 26 '14 at 13:42

1 Answers1

0

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.

Community
  • 1
  • 1
JoelC
  • 3,664
  • 9
  • 33
  • 38
  • Ahh right, thank you very much. Well would there be an easier way to update my application? Or could I do what I was normally going to do but in the Library? –  Aug 25 '14 at 21:04
  • Also, when compiling my project, would I have to include the App Manifest as well as the EXE? –  Aug 25 '14 at 21:11
  • The Application Manifest is already compiled into your application when you build it. If you didn't want your entire application to have elevated, you could have a mini copy utility that you use `Process.Start` to run, then you just have to set the properties on your `StartInfo` as show here: http://stackoverflow.com/questions/562350/requested-registry-access-is-not-allowed/562389#562389 – JoelC Aug 25 '14 at 21:15
  • I have looked at that and changed it to `requireAdministrator` however, it has not worked? Do I have to include the manifest when compiling it with Install Shield? Or is it 'combined' with the .exe automatically? I have looked everywhere and it also says to turn 'disable' one ClickOnce setting in the Application Properties, I have done that and it has also not worked. –  Aug 26 '14 at 08:43