August 1, 2012

C# File OpenAs Dialog (Win7)

Some time ago I ran into something weird. These lines of code worked perfectly the last years in order to show the open as dialog on Windows XP but stopped working on my x64 Windows 7 machine (it would have stopped working in Vista too, but I skipped this version).
ProcessStartInfo startInfo=new ProcessStartInfo() { WindowStyle=ProcessWindowStyle.Normal, FileName=fileName, Verb="openas", UseShellExecute=true, ErrorDialog=true, }; p=Process.Start(startInfo);
Well to solve this issue I needed to check how the system used to show the open as dialog. As a result, I ended up with this line of code which brings up the open as dialog as usual.
p=Process.Start("rundll32.exe", string.Format("shell32,OpenAs_RunDLL \"{0}\"", fileName));
By the way, you'll need both versions if you want that your application works on Windows XP and later versions. This line indicates whether the application is running on Windows Vista or later.
Environment.OSVersion.Platform==PlatformID.Win32NT && Environment.OSVersion.Version>=new Version(6, 0, 6000);

2 comments:

  1. Thank you very much for posting this. It's kind of a nasty work around in my humble opinion, but it resolves an even more irritating problem.

    ReplyDelete
  2. Thanks for your comment, I'm glad this solution helped you too. You're right, it's very irritating that the 'openas' mechanism has changed so it doesn't work the same way as it did before.

    ReplyDelete