VSRegistry.cs :  » Development » StyleCop » Microsoft » VisualStudio » Shell » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Development » StyleCop 
StyleCop » Microsoft » VisualStudio » Shell » VSRegistry.cs
//------------------------------------------------------------------------------
// <copyright file="Package.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>                                                                
//------------------------------------------------------------------------------
using System;
using Microsoft.Win32;

using Microsoft.VisualStudio.Shell.Interop;

using ErrorHandlerMicrosoft.VisualStudio.ErrorHandler;
using IServiceProviderSystem.IServiceProvider;
using IOleServiceProviderMicrosoft.VisualStudio.OLE.Interop.IServiceProvider;

namespace Microsoft.VisualStudio.Shell{
    /// <summary>
    /// Helper class to handle the registry of the instance of VS that is
    /// hosting this code.
    /// </summary>
    [System.CLSCompliant(false)]
    public static class VSRegistry {
        // ServiceProvider object that wraps the global service provider of VS.
        private static ServiceProvider globalProvider;

        private static ServiceProvider GlobalProvider {
            get {
                if (null == globalProvider) {
                    // Try to get the global service provider from the Package class.
                    IOleServiceProvider sp = Package.GetGlobalService(typeof(IOleServiceProvider)) as IOleServiceProvider;
                    if (null != sp) {
                        globalProvider = new ServiceProvider(sp);
                    }
                }
                return globalProvider;
            }
        }

        /// <summary>
        /// Returns a read-only RegistryKey object for the root of a given storage type.
        /// It is up to the caller to dispose the returned object.
        /// </summary>
        /// <param name="registryType">The type of registry storage to open.</param>
        public static RegistryKey RegistryRoot(__VsLocalRegistryType registryType) {
            return RegistryRoot(GlobalProvider, registryType, false);
        }

        /// <summary>
        /// Returns a RegistryKey object for the root of a given storage type.
        /// It is up to the caller to dispose the returned object.
        /// </summary>
        /// <param name="registryType">The type of registry storage to open.</param>
        /// <param name="writable">Flag to indicate is the key should be writable.</param>
        public static RegistryKey RegistryRoot(__VsLocalRegistryType registryType, bool writable) {
            return RegistryRoot(GlobalProvider, registryType, writable);
        }

        /// <summary>
        /// Returns a RegistryKey object for the root of a given storage type.
        /// It is up to the caller to dispose the returned object.
        /// </summary>
        /// <param name="provider">The service provider to use to access the Visual Studio's services.</param>
        /// <param name="registryType">The type of registry storage to open.</param>
        /// <param name="writable">Flag to indicate is the key should be writable.</param>
        public static RegistryKey RegistryRoot(IServiceProvider provider, __VsLocalRegistryType registryType, bool writable) {
            if (null == provider) {
                throw new ArgumentNullException("provider");
            }

            // The current implementation of the shell supports only RegType_UserSettings and
            // RegType_Configuration, so for any other values we have to return not implemented.
            if ((__VsLocalRegistryType.RegType_UserSettings != registryType) &&
                (__VsLocalRegistryType.RegType_Configuration != registryType))
            {
                throw new NotSupportedException();
            }

            // Try to get the new ILocalRegistry4 interface that is able to handle the new
            // registry paths.
            ILocalRegistry4 localRegistry = provider.GetService(typeof(SLocalRegistry)) as ILocalRegistry4;
            if (null != localRegistry) {
                uint rootHandle;
                string rootPath;
                if (ErrorHandler.Succeeded(localRegistry.GetLocalRegistryRootEx((uint)registryType, out rootHandle, out rootPath))) {
                    // Check if we have valid data.
                    __VsLocalRegistryRootHandle handle = (__VsLocalRegistryRootHandle)rootHandle;
                    if (!string.IsNullOrEmpty(rootPath) && (__VsLocalRegistryRootHandle.RegHandle_Invalid != handle)) {
                        // Check if the root is inside HKLM or HKCU. Note that this does not depends only from
                        // the registry type, but also from instance-specific data like the RANU flag.
                        RegistryKey root = (__VsLocalRegistryRootHandle.RegHandle_LocalMachine == handle) ? Registry.LocalMachine : Registry.CurrentUser;
                        return root.OpenSubKey(rootPath, writable);
                    }
                }
            }

            // We are here if the usage of the new interface failed for same reason, so we have to fall back to
            // the ond way to access the registry.
            ILocalRegistry2 oldRegistry = provider.GetService(typeof(SLocalRegistry)) as ILocalRegistry2;
            if (null == oldRegistry) {
                // There is something wrong with this installation or this service provider.
                return null;
            }
            string registryPath;
            NativeMethods.ThrowOnFailure(oldRegistry.GetLocalRegistryRoot(out registryPath));
            if (string.IsNullOrEmpty(registryPath)) {
                return null;
            }

            RegistryKey regRoot = (__VsLocalRegistryType.RegType_Configuration == registryType) ? Registry.LocalMachine : Registry.CurrentUser;
            return regRoot.OpenSubKey(registryPath, writable);
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.