Reputation: 2566
I have a string that is a data URI. For example
string dataURI = data:text/html,<html><p>This is a test</p></html>
Then I call the web browser by using
System.Diagnostics.Process.Start(dataURI)
But the web browser doesn't open, I just get an error. When I paste my data URI into the browser adress bar, it opens the page just fine.
Can anyone please help me and tell me what I'm doing wrong?
Thank you, Tony
Upvotes: 0
Views: 1089
Reputation: 3388
As per this article,
ShellExecute parses the string that is passed to it so that ShellExecute can extract either a protocol specifier or an extension. Next, ShellExecute looks in the registry and then uses either the protocol specifier or the extension to determine which application to start. If you pass http://www.microsoft.com to ShellExecute, ShellExecute recognizes the http:// sub-string as a protocol.
In your case there is no http
substring. So you have to explicitly pass the default browser executable as file name and data URI as parameter. I used the GetBrowserPath
code from this article.
string dataURI = "data:text/html,<html><p>This is a test</p></html>";
string browser = GetBrowserPath();
if(string.IsNullOrEmpty(browser))
browser = "iexplore.exe";
System.Diagnostics.Process p = new Process();
p.StartInfo.FileName = browser;
p.StartInfo.Arguments = dataURI;
p.Start();
private string GetBrowserPath()
{
string browser = string.Empty;
Microsoft.Win32.RegistryKey key = null;
try
{
// try location of default browser path in XP
key = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(@"HTTP\shell\open\command", false);
// try location of default browser path in Vista
if (key == null)
{
key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http", false); ;
}
if (key != null)
{
//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);
}
}
}
finally
{
if (key != null) key.Close();
}
return browser;
}
Upvotes: 2