5

I am programming a Windows Form Application and I need to programmatically get back to the Desktop.

I tried this code, but it is not working:

using System;
using System.Windows.Forms;

private void ToggleDesktop() {

SendKeys.Send("^({ESC}D)"); //<-- Semantic error, Should simulate: WIN+D

}

Is there a possibility to do that?

Yotam Salmon
  • 2,400
  • 22
  • 36
FreaX
  • 94
  • 1
  • 9

2 Answers2

12

You can use the Shell32.dll windows assembly to do that.

Just add a reference to C:\Windows\System32\Shell32.dll, then go to the reference properties and put a False near Embed Interop Types (since the class you're going to use is ShellClass, which is interop.

Now, it's as simple as

Shell32.ShellClass objShel = new Shell32.ShellClass();
objShel.ToggleDesktop();
Yotam Salmon
  • 2,400
  • 22
  • 36
3

Also possible (tested under Windows 8.1):

Add COM reference to "Microsoft Shell Controls and Automation"
(c:\windows\system32\shell32.dll)

Then:

using Shell32;

Shell shellObject = new Shell();
shellObject.ToggleDesktop(); // WinXp: ((Shell32.IShellDispatch4)shellObject).ToggleDesktop();
Pollitzer
  • 1,580
  • 3
  • 18
  • 28