added some development tools
[windows-sources.git] / developer / VSSDK / VisualStudioIntegration / Common / Source / CSharp / Shell100 / VSRegistry.cs
blob8e8396b148ed4920855a8ffc2ff6c73b970f13d3
1 //------------------------------------------------------------------------------
2 // <copyright file="Package.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6 using System;
7 using Microsoft.Win32;
9 using Microsoft.VisualStudio.Shell.Interop;
11 using ErrorHandler = Microsoft.VisualStudio.ErrorHandler;
12 using IServiceProvider = System.IServiceProvider;
13 using IOleServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider;
15 namespace Microsoft.VisualStudio.Shell {
16 /// <summary>
17 /// Helper class to handle the registry of the instance of VS that is
18 /// hosting this code.
19 /// </summary>
20 [System.CLSCompliant(false)]
21 public static class VSRegistry {
23 private static ServiceProvider GlobalProvider {
24 get {
25 return ServiceProvider.GlobalProvider;
29 /// <summary>
30 /// Returns a read-only RegistryKey object for the root of a given storage type.
31 /// It is up to the caller to dispose the returned object.
32 /// </summary>
33 /// <param name="registryType">The type of registry storage to open.</param>
34 public static RegistryKey RegistryRoot(__VsLocalRegistryType registryType) {
35 return RegistryRoot(GlobalProvider, registryType, false);
38 /// <summary>
39 /// Returns a RegistryKey object for the root of a given storage type.
40 /// It is up to the caller to dispose the returned object.
41 /// </summary>
42 /// <param name="registryType">The type of registry storage to open.</param>
43 /// <param name="writable">Flag to indicate is the key should be writable.</param>
44 public static RegistryKey RegistryRoot(__VsLocalRegistryType registryType, bool writable) {
45 return RegistryRoot(GlobalProvider, registryType, writable);
48 /// <summary>
49 /// Returns a RegistryKey object for the root of a given storage type.
50 /// It is up to the caller to dispose the returned object.
51 /// </summary>
52 /// <param name="provider">The service provider to use to access the Visual Studio's services.</param>
53 /// <param name="registryType">The type of registry storage to open.</param>
54 /// <param name="writable">Flag to indicate is the key should be writable.</param>
55 public static RegistryKey RegistryRoot(IServiceProvider provider, __VsLocalRegistryType registryType, bool writable) {
56 if (null == provider) {
57 throw new ArgumentNullException("provider");
60 // The current implementation of the shell supports only RegType_UserSettings and
61 // RegType_Configuration, so for any other values we have to return not implemented.
62 if ((__VsLocalRegistryType.RegType_UserSettings != registryType) &&
63 (__VsLocalRegistryType.RegType_Configuration != registryType))
65 throw new NotSupportedException();
68 // Try to get the new ILocalRegistry4 interface that is able to handle the new
69 // registry paths.
70 ILocalRegistry4 localRegistry = provider.GetService(typeof(SLocalRegistry)) as ILocalRegistry4;
71 if (null != localRegistry) {
72 uint rootHandle;
73 string rootPath;
74 if (ErrorHandler.Succeeded(localRegistry.GetLocalRegistryRootEx((uint)registryType, out rootHandle, out rootPath))) {
75 // Check if we have valid data.
76 __VsLocalRegistryRootHandle handle = (__VsLocalRegistryRootHandle)rootHandle;
77 if (!string.IsNullOrEmpty(rootPath) && (__VsLocalRegistryRootHandle.RegHandle_Invalid != handle)) {
78 // Check if the root is inside HKLM or HKCU. Note that this does not depends only from
79 // the registry type, but also from instance-specific data like the RANU flag.
80 RegistryKey root = (__VsLocalRegistryRootHandle.RegHandle_LocalMachine == handle) ? Registry.LocalMachine : Registry.CurrentUser;
81 return root.OpenSubKey(rootPath, writable);
86 // We are here if the usage of the new interface failed for same reason, so we have to fall back to
87 // the ond way to access the registry.
88 ILocalRegistry2 oldRegistry = provider.GetService(typeof(SLocalRegistry)) as ILocalRegistry2;
89 if (null == oldRegistry) {
90 // There is something wrong with this installation or this service provider.
91 return null;
93 string registryPath;
94 NativeMethods.ThrowOnFailure(oldRegistry.GetLocalRegistryRoot(out registryPath));
95 if (string.IsNullOrEmpty(registryPath)) {
96 return null;
99 RegistryKey regRoot = (__VsLocalRegistryType.RegType_Configuration == registryType) ? Registry.LocalMachine : Registry.CurrentUser;
100 return regRoot.OpenSubKey(registryPath, writable);