[This documentation is for preview only, is not supported, and is subject to change in later releases.]

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

using System.Xml;

using System.Web;

using System.Threading;

using System.Net;

using Microsoft.Win32;

using System.Diagnostics;

using Microsoft.VisualStudio.Shell.Interop;

using EnvDTE100;

using EnvDTE;

 

namespace MS.SDKMSDNKeywordIndex

 

{

    class HelpUtility

    {

        // Get Help XML as a stream

        public static Stream GetHelpXmlAsStream(string helpUrl)

        {

            WebRequest webRequestObj = WebRequest.Create(helpUrl);

            return webRequestObj.GetResponse().GetResponseStream();

        }

        // Get Help XML as a string

        public static string GetHelpXmlAsString(string helpUrl)

        {

            try

            {

                string xml = "";

                Stream webStream = GetHelpXmlAsStream(helpUrl);

                StreamReader webReader = new StreamReader(webStream);

 

                string sLine = "";

                while (sLine != null)

                {

                    sLine = webReader.ReadLine();

                    if (sLine != null)

                        xml += sLine;

                }

 

                webStream.Close();

                return xml;

            }

            catch

            {

                throw;

            }

        }

 

        // Get Help XML as a XML document

        public static XmlDocument GetHelpXmlAsXmlDocument(string helpUrl)

        {

            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.Load(GetHelpXmlAsStream(helpUrl));

            return xmlDoc;

        }

 

        public static void LaunchBrowserHelp(string Url)

        {

            System.Diagnostics.Process proc = null;

            using (proc = new System.Diagnostics.Process())

            {

                proc.EnableRaisingEvents = false;

                proc.StartInfo.FileName = getDefaultBrowser();

                proc.StartInfo.Arguments = Url;

                proc.Start();

            }

        }

 

        public static void LaunchIDEBrowserHelp(string Url)

        {

            // leverage the VSPackage service call to get a hold if the IDE Browser.

            IVsWebBrowsingService webService = (IVsWebBrowsingService)SDKMSDNKeywordIndexPackage.GetGlobalService(typeof(IVsWebBrowsingService));

            if (webService != null)

            {

                IVsWindowFrame window;

                //using AddToMRU, but could easily fire a new tab for each call.

                __VSWBNAVIGATEFLAGS flags = __VSWBNAVIGATEFLAGS.VSNWB_AddToMRU;

                webService.Navigate(Url, (uint)flags, out window);

                return;

            }

 

        }

 

        private static string getDefaultBrowser()

        {

           

            string browser = string.Empty;

            RegistryKey key = null;

 

            using (key = Registry.ClassesRoot.OpenSubKey(@"HTTP\shell\open\command", false))

            {

                //trim off quotes

                browser = key.GetValue(null).ToString().ToLower().Replace("\"", "");

                if (!browser.EndsWith("exe"))

                {

                    //get rid of everything after the ".exe"

                    browser = browser.Substring(0, browser.LastIndexOf(".exe") + 4);

                }

            }

 

            return browser;

        }

 

        private static string getAgentLocation()

        {

            string agent = string.Empty;

            RegistryKey key = null;

 

            using (key = Registry.ClassesRoot.OpenSubKey(@"MS-XHelp\shell\open\command", false))

            {

                //trim off quotes

                agent = key.GetValue(null).ToString().ToLower().Replace("\"", "");

                if (!agent.EndsWith("exe"))

                {

                    //get rid of everything after the ".exe"

                    agent = agent.Substring(0, agent.LastIndexOf(".exe") + 4);

                }

            }

 

            return agent;

        }

 

        private static bool IsProcessOpen(string name)

        {

            foreach (System.Diagnostics.Process clsProcess in System.Diagnostics.Process.GetProcesses())

            {

 

                if (clsProcess.ProcessName.Contains(name))

                {

                    return true;

                }

            }

            return false;

        }

 

        public static void StartHelpAgent()

        {

            System.Diagnostics.Process proc = null;

            if (IsProcessOpen("HelpLibAgent") == false)

            {

                using (proc = new System.Diagnostics.Process())

                {

                    proc.EnableRaisingEvents = false;

                    proc.StartInfo.FileName = getAgentLocation();

                    proc.Start();

                }

            }

        }

    }

 

}