1

I am writing a VSPackage for Visual Studio 2013. This package's supposed to download some XML Schema files, then copy them to Visual Studio's %installation folder%\Xml\Schemas.

I use WebClient to download the files. It works perfectly when I set the destination folder to some folder like C:\Test. Here is the code that does this:

webclient.DownloadFileAsync(url, "% Destination Folder Here %");

Problem:

When I set the destination foder to "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Xml\Schemas", I get the following error:

Access to the path 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Xml\Schemas' is denied.

I have tried:

1 - Changing the folder permissions manually in Windows.

2 - (I prefer to do this one) Changing the access permission in the code with C#:

ApplicationVariables.SCHEMA_PATH = @"C:\Program Files (x86)\Microsoft Visual Studio 12.0\Xml\Schemas";

//  Set access to the schema directory.
DirectoryInfo schemaDirectoryInfo = new DirectoryInfo(ApplicationVariables.SCHEMA_PATH);
DirectorySecurity schemadirectorySecurity = schemaDirectoryInfo.GetAccessControl();
schemadirectorySecurity.AddAccessRule(
     new FileSystemAccessRule(
            new SecurityIdentifier(WellKnownSidType.WorldSid, null),
            FileSystemRights.FullControl,
            AccessControlType.Allow)
            );

// I also have tried this
        FileIOPermission filePermission = new FileIOPermission(FileIOPermissionAccess.AllAccess, ApplicationVariables.SCHEMA_PATH);
        try
        {
            filePermission.Demand();
        }
        catch (Exception ex)
        {
            Debug.WriteLine("Security Error = " + ex.Message);
        }

Question

How can my application copy some files into Visual Studio installation folder?

H H
  • 263,252
  • 30
  • 330
  • 514
A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
  • 3
    Have you tried doing a `Run as administrator` on your application? – sstan Jun 15 '15 at 20:22
  • 1
    You need to elevate your process to an administrator group (as sstan said) to be able to write to the Program Files (or Program File x86) directories. Doesn't matter if you change the permissions on the folder. – Ron Beyer Jun 15 '15 at 20:25
  • I'm running the code in debug mode. How can I debug/compile the code as an Admin? thanks – A-Sharabiani Jun 15 '15 at 20:28
  • 2
    If you are running in debug mode, then I believe you need to run Visual Studio itself in `Run as administrator` mode. – sstan Jun 15 '15 at 20:31
  • @sstan already tried. still the same error. – A-Sharabiani Jun 15 '15 at 20:34
  • 2
    Running VS in Administrator mode doesn't elevate your process to administrator. In order to elevate your process you need to add some information to the app.config file. See http://stackoverflow.com/questions/2818179/how-to-force-my-net-app-to-run-as-administrator-on-windows-7 – Ron Beyer Jun 15 '15 at 20:37
  • Well, I also read [this](http://stackoverflow.com/questions/20404231/how-to-run-application-as-administrator-in-debug-with-visual-studio-2010), which says that running VS in administrator mode does the trick. And I just performed the test of trying to write under `c:\Program Files (x86)`, and doing `Run as administrator` on VS did the trick. Without it, I got the access denied exception. (I used VS 2013, in case it makes a difference) – sstan Jun 15 '15 at 20:39
  • Bon Beyer: since this is a VSPackage, there is no `app.config` file. – A-Sharabiani Jun 15 '15 at 20:40
  • Oh snap, a VSPackage. I didn't test that. Have you tried to make it work as a simple application with a `main()`? Can you make it work then? Is your problem specific to VSPackages? – sstan Jun 15 '15 at 20:42

1 Answers1

2

Admin rights are required for that, and VS 2008 or higher doesn't run elevated (with admin rights) by default, the user should launch VS using the "Run as administrator" context menu entry. A package shouldn't force the user to do this, which means that a package cannot copy files to C:\Program Files (x86) (or to write to registry hive HKEY_LOCAL_MACHINE).

The setup of your package can do that (setups can run with admin rights by default if designed so), but your package cannot.

Carlos Quintero
  • 4,300
  • 1
  • 11
  • 18
  • I ran Visual Studio as `Admin`, the VSPackage was able to copy files. Now, if the user is NOT running the Visual Studio as an admin, how can my VSPackage ask the user for admin privileges or somehow copy the files? – A-Sharabiani Jul 03 '15 at 18:22