Posts filed under Windows 8

SOLVED - How to open a URL in the default browser in c#

For launching a URL in the default browser, I used to write this simple c# code:

Process.Start("http://www.royal-server.com/");

But during my tests today I got a nice exception when i execute this code:

 

System.ComponentModel.Win32Exception was unhandled
HResult=-2147467259
Message=The system cannot find the file specified
Source=System
ErrorCode=-2147467259
NativeErrorCode=2
StackTrace:
 at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
 at System.Diagnostics.Process.Start()
 at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
 at System.Diagnostics.Process.Start(String fileName)
 at ConsoleApplication6.Program.Main(String[] args) in c:\Users\michaelseirer\Documents\Visual Studio 2013\Projects\ConsoleApplication6\ConsoleApplication6\Program.cs:line 20
... 

Now, it seems that this code in previous versions of Windows worked. And the reason, why it is not working on my system is simply because I have Chrome as my default browser... (the code works with Firefox as default browser)

Setting it back to IE solved the problem:

But: who wants this? ... ;)

 

So I googled a bit and found code, that reads the path to the current default browser in the Registry in HKEY_CLASSES_ROOT\http:

But - lo and behold! - the subtree does not change if you change to another default browser. This registry-place seems to be a dead end (it maybe was used in earlier versions of Windows?) This is also mentioned here in this thread at a Microsoft forum.

 

The Solution

Starting with Windows Vista a new "Default Programs" was introduced. In this case, the value a user can set his own choice of browser (per user, not per machine!) - and it will be saved under the following registry key: 

HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice\Progid

There, you can find a string that identifies the browser:

  • FirefoxURL - for Firefox
  • ChromeHTML - for Chrome
  • IE.HTTP - for Internet Explorer
  • and probably some more (but I have not tested others)

The value here is a pointer to HKEY_CLASSES_ROOT\<Name> , e.g. HKEY_CLASSES_ROOT\IE.HTTP. The concrete starting path to the executable can be found here: HKEY_CLASSES_ROOT\<name>\shell\open\command 

 

This led to the following - though in my opinion huge amount of code - to actually launch a Url in the default browser:

public static string GetDefaultBrowserPath()
{
string urlAssociation = @”Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http”;
string browserPathKey = @”$BROWSER$\shell\open\command”;

RegistryKey userChoiceKey = null;
string browserPath = “”;

try
{
//Read default browser path from userChoiceLKey
userChoiceKey = Registry.CurrentUser.OpenSubKey(urlAssociation + @”\UserChoice”, false);

//If user choice was not found, try machine default
if (userChoiceKey == null)
{
//Read default browser path from Win XP registry key
var browserKey = Registry.ClassesRoot.OpenSubKey(@”HTTP\shell\open\command”, false);

//If browser path wasn’t found, try Win Vista (and newer) registry key
if (browserKey == null)
{
browserKey =
Registry.CurrentUser.OpenSubKey(
urlAssociation, false);
}
var path = CleanifyBrowserPath(browserKey.GetValue(null) as string);
browserKey.Close();
return path;
}
else
{
// user defined browser choice was found
string progId = (userChoiceKey.GetValue(“ProgId”).ToString());
userChoiceKey.Close();

// now look up the path of the executable
string concreteBrowserKey = browserPathKey.Replace(“$BROWSER$”, progId);
var kp = Registry.ClassesRoot.OpenSubKey(concreteBrowserKey, false);
browserPath = CleanifyBrowserPath(kp.GetValue(null) as string);
kp.Close();
return browserPath;
}
}
catch(Exception ex)
{
return “”;
}
}

This way, you open the URL in the browser the user actually specified (or that is configured system-wide - depending on the OS).

 

Process.Start(GetDefaultBrowserPath(), url);

 

These have been the resources I found useful during my research and that helped me gathering the function above:

 

 

REMARK 06/11/15:

After updating to the latest Google Chrome version (in my case I updated from 35.0.1916.114 to 35.0.1916.153) Process.Start worked also for Google Chrome. So it seems that only some versions of Chrome had this issue.

 

 

 

 

 

Posted on June 11, 2014 and filed under development, Windows 8.

Highlights von der Build 2014 - .NET User Group Austria

In the context of the .NET User Group Austria Christian Nagel held a nice presentation about the news from the //build/ 2014. It was a very subjective listing and on purpose he did not try to be very objective with his selection. Which would not be possible given the 1.5h time frame ;)

Don't forget that you can watch all the sessions online at Channel9


1. Internet of Things

Since the forecasts of the number of devices connected to the internet increase every month its clear that Microsoft also is not missing this train and presented numerous efforts in this area: Windows Embedded compact 2013, Windows Embedded and the .NET micro framework.


2. Best session: "IT'S A TRAP - 3 Remarkably Common UI Traps and How to Avoid Them in Your Designs"

For Christian, the best session of the whole //build was the "Its a trap!" session of Steve Herbst and Michael Medlock. View this session - its highly recommended.


3. .NET 4.5.1

Although some of the participants were developing with .NET 4.5.1 already, Christian presented the new features of 4.5.1 like 64-bit edit and continue, method return value inspection, async debugging enhancements, ASP.NET application suspension, multicore JIT improvements, on-demand large heap compaction and many more. For an in-depth session I recommend watching "The Next Generation of .NET for Building Applications" by Habib Heydarian.


4. "Roslyn"

With Roslyn, Microsoft did a complete rewrite of the c# and the VB compiler and exposing them as rich APIs for everyone to use. This enables every developer for a better development experience, VS extensions are easier to write now and since its a complete new architecture its easier to extend and maintain for Microsoft. Also, its fully open source now! The session "The future of C#" highlights in depth knowledge about Roslyn. 


5. C# language features - vnext

At the end we got a glimpse of how the next version of C# will look like. Microsoft and the community had a lot of ideas how the language can be improved. But many of them were complicated to develop in the old compiler. So with Roslyn, Microsoft is now in the position to realise them. Here you can see the status of all these improvements. 

Just to tease you a bit:

  • Primary constructors
  • Auto-property initializers
  • Getter-only auto-properties
  • Dictionary initializers
  • Await in catch/finally
  • Null propagation


6. Wrap up

Eventhough I am not liking some of the new C# language features - or it might be that I don't fully understand them? ;) - it was good to hear from Christian his own impressions and what he found noteworthy from the //build. Max Knor, who also listened to the presentation, recommended his own topics from the //build/ while he helped us killing the provided Pizza after the presentation ;) 

Mouse without Borders with Win8 support (.net 4.5)

I upgraded my server to Windows Server 2012 (finally i found the time) and was astonished when MouseWithoutBorders informed me, that i could not install it because i was missing the .NET framework 2.0 (!) - since its a fresh Windows Server 2012 .NET 4.5 was installed by default ...

Some googling and poking around in forums i found the updated version. The confusion just came up because top ranked pages in Google are still featuring the old version.

Here at Microsoft you can find the version that supports Windows8 (and runs with .NET framework 4). Also if you use Chocolatey (see my previous post) you get the latest version easily without all that hassle!

Posted on January 13, 2013 and filed under Windows 8, productivity.

Windows 8 Dev Camp Vienna review

Since it was advertised for "Expert programmers" i gave Microsoft a chance and spent 1 full day at the Windows 8 Dev Camp at Microsoft.

The first presentation was more ...lets say "marketing slides driven", but it nevertheless contained some good tips:

  • Thomas Mutzl prepared some non-trivial examples of Windows Store apps: one for a Video App and one for a Maps App. A third one is on the way which deals with RSS feeds.
  • all presentations from the Dev Camp (and much more) can be downloaded at Windows 8 Camp in a box
  • If you know already your next cool Windows 8 App but didnt find the time to code it yet, at least you should reserve your app-name

The second part was much more code-focused and contained some interesting details

Posted on December 6, 2012 and filed under Windows 8.