Search code examples
c#visual-studiovisual-studio-extensionsvspackage

How to give Administrator Privileges to my VSPackage?


I've developed a VSPackage for Visual Studio, which needs to copy some files to Visual Studio's Installation Path.

If I Run Visual Studio as Administrator (when using my installed VSPackage). It can copy files with no errors.

When I run Visual Studio normally, I get Access to the path ... is denied error.

Question

How can I give my VSPackage to admin privileges even when the Visual Studio is being run as a normal user.

Or at least how can I invoke something like this:

enter image description here


Solution

  • Your package is a .dll (loaded on a process), not an .exe (a process), and therefore it cannot have different privileges than its process (Visual Studio, that is, devenv.exe). What your package can do is to launch a different process with admin rights. See my article:

    HOWTO: Launch a process with admin rights from a Visual Studio add-in on Windows Vista or higher.

    That said, it is a very wrong approach to do this to copy files to the VS installation path. That should be done by the setup of your package (.msi), not by your package.

    On the one hand, if it is done by the package once installed, the user could deny the elevation prompt and the files would not be copied and your installation would be incomplete. Can your package run properly without those files?

    On the other hand, if the user denies the elevation prompt to install the package, it wouldn't be installed at all, which is a more clean approach.