3

Well, for example I have some simple console application. My purpose is to generate some report and represent it to the user in some external application such as notepad or a web browser.

class Program
    {
        [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        [DllImport("User32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);

        static void Main()
        {
            OpenNotepadAndInputTextInIt("Message in notepad");
            OpenHtmlFileInBrowser(@"c:\temp\test.html");
        }

        private static string GetDefaultBrowserPath()
        {
            string key = @"HTTP\shell\open\command";
            using(RegistryKey registrykey = Registry.ClassesRoot.OpenSubKey(key, false))
            {
                return ((string)registrykey.GetValue(null, null)).Split('"')[1];
            }
        }

        private static void OpenHtmlFileInBrowser(string localHtmlFilePathOnMyPc)
        {
            Process browser = Process.Start(new ProcessStartInfo(GetDefaultBrowserPath(), string.Format("file:///{0}", localHtmlFilePathOnMyPc)));
            browser.WaitForInputIdle();

        }

        private static void OpenNotepadAndInputTextInIt(string textToInputInNotepad)
        {
            Process notepad = Process.Start(new ProcessStartInfo("notepad.exe"));
            notepad.WaitForInputIdle();
            if(notepad != null)
            {
                IntPtr child = FindWindowEx(notepad.MainWindowHandle, new IntPtr(0), "Edit", null);
                SendMessage(child, 0x000C, 0, textToInputInNotepad);
            }
        }
    }

This solution works fine, But as you can see I have two methods; GetDefaultBrowserPath() and GetDefaultBrowserPath(string localHtmlFilePathInMyPc). The first one passes message string directly to notepad window, but the second one needs to create a file, some html page, and than pass this html file as parameter for the web browser. This is kind of a slow solution. I want to generate an html string report and pass it directly to a web browser without creating intermediate html files.

1 Answer 1

2

If you want to open an external browser window (rather than integrating one into your application) in the system’s default browser application I think the only way is going via a data URI:

data:text/html,<html><title>Hello</title><p>This%20is%20a%20test

Pass this as the URI into the browser. However, I wouldn’t really advise doing this. It’s unexpected for the user and there isn’t really a disadvantage in generating a temporary file to be displayed, is there? In addition, you might start hitting restrictions on the URI length pretty quickly.

Incidentally, your current way of opening a file in the browser is way more complicated than required. Shell execution does the right thing on its own, no need to retrieve the browser’s path from the registry manually:

Process.Start("file:///the/path/here")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.