3

I am trying to open a web browser via the following methods. However, when the browser opens the url / file path, the fragment piece gets mangled (from "#anchorName" to "%23anchorName") which does not seem to get processed. So basically, the file opens but does not jump to the appropriate location in the document. Does anyone know how to open the file and have the fragment processed? Any help on this would be greatly appreciated.

an example path to open would be "c:\MyFile.Html#middle"

    // calls out to the registry to get the default browser
    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];
       }
    }

    // creates a process and passes the url as an argument to the process
    private static void Navigate(string url)
    {
       Process p = new Process();
       p.StartInfo.FileName = GetDefaultBrowserPath();
       p.StartInfo.Arguments = url;
       p.Start();
    }
user248450
  • 81
  • 1
  • 1
  • 5

4 Answers4

5

Thanks to all that tried to help me with this issue. I have since found a solution that works. I have posted it below. All you need to do is call navigate with a local file path containing a fragment. Cheers!

    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 Navigate(string url)
    {
       Process.Start(GetDefaultBrowserPath(), "file:///{0}".FormatWith(url));
    }
user248450
  • 81
  • 1
  • 1
  • 5
3

All you need is:

System.Diagnostics.Process.Start(url);
arviman
  • 5,087
  • 41
  • 48
1

Try relying on the system to resolve things correctly:

    static void Main(string[] args)
    {
        Process p = new Process();
        p.StartInfo.UseShellExecute = true;
        p.StartInfo.FileName = "http://stackoverflow.com/questions/tagged/c%23?sort=newest&pagesize=50";
        p.StartInfo.Verb = "Open";
        p.Start();
    }
Philipp Schmid
  • 5,778
  • 5
  • 44
  • 66
  • thanks for the info phillip, but the example you have provided here is an link to an external website. i am opening a file on my hard drive which does not use the same process. when its a local file the process looks to the registry to determine what default program is used to open a file with the given extension. because your example is external web page, windows can determine that its dealing with an url and handle it appropriately. Above, im forcing some of that by looking up the users preferred program since windows cant decipher the extension when there is a fragment – user248450 Aug 26 '11 at 13:36
  • 1
    I just tried this for good measure and the fragment gets stripped using @"C:\Test.html#middle" results in "file:///C:/Test.html" thanks again for your suggestion. – user248450 Aug 26 '11 at 13:42
  • 1
    It actually crashes the app with a file not found exception. i tried it the first time without the fragment. woops. – user248450 Aug 26 '11 at 13:53
0

I am not a C# programmer, but in PHP I would do a urlencode. When I did a Google search on C# and urlencode it gave this page here at StackOverflow... url encoding using C#

Community
  • 1
  • 1
user424678
  • 41
  • 1
  • That doesn't make any sense, it's a URL already, he's not appending parameters in this code... – Ry- Aug 26 '11 at 22:07