Access denied when copying files to Visual Studio's installation folder

2k views Asked by At

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?

1

There are 1 answers

1
Carlos Quintero On BEST ANSWER

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.