title: Creating/Switching Desktop in C#/.Net
body: <p>I'm currently using the CreateDesktop native C function and calling it in my C# code to create and switch between desktops.  Is there any way to do this using the Process class or any c#/.Net class for that matter?</p>
  
  <p>This is the sample code i'm using in my class now for Desktop switching.</p>
  
  <pre><code>    [Flags]
      public enum AccessRight : uint
      {
          DESKTOP_READOBJECTS = 0x00000001,
          DESKTOP_CREATEWINDOW = 0x00000002,
          DESKTOP_CREATEMENU = 0x00000004,
          DESKTOP_HOOKCONTROL = 0x00000008,
          DESKTOP_JOURNALRECORD = 0x00000010,
          DESKTOP_JOURNALPLAYBACK = 0x00000020,
          DESKTOP_ENUMERATE = 0x00000040,
          DESKTOP_WRITEOBJECTS = 0x00000080,
          DESKTOP_SWITCHDESKTOP = 0x00000100,
  
          GENERIC_ALL = (DESKTOP_READOBJECTS | DESKTOP_CREATEWINDOW | DESKTOP_CREATEMENU |
              DESKTOP_HOOKCONTROL | DESKTOP_JOURNALRECORD | DESKTOP_JOURNALPLAYBACK |
              DESKTOP_ENUMERATE | DESKTOP_WRITEOBJECTS | DESKTOP_SWITCHDESKTOP)
      };
  
      [Flags]
      public enum AccountHook
      {
          Allow = 1,
          Disallow = 0
      };
  
      public enum HandleInheritance
      {
          Inherit,
          None
      };
  
      [StructLayout(LayoutKind.Sequential)]
      public struct SecAttrib
      {
          public int nLength;
          public IntPtr lpSecurityDescriptor;
          public int bInheritHandle;
      }
  
      [DllImport("user32.dll")]
      public static extern IntPtr OpenDesktop(string lpszDesktop, 
          uint dwFlags, 
          bool fInherit, 
          uint dwDesiredAccess);
  
      [DllImport("user32.dll")]
      public static extern bool SwitchDesktop(IntPtr hDesktop);
  
      [DllImport("user32.dll")]
      public static extern IntPtr CreateDesktop(string lpszDesktop, 
          IntPtr lpszDevice, 
          IntPtr pDevmode,
          int dwFlags, 
          uint dwDesiredAccess, 
          IntPtr lpsa);
  
      [DllImport("user32.dll", SetLastError = true)]
      public static extern IntPtr OpenInputDesktop(uint dwFlags, bool fInherit,
         uint dwDesiredAccess);
  
  
  
      [DllImport("user32.dll", EntryPoint = "CloseDesktop", CharSet = CharSet.Unicode, SetLastError = true)]
      [return: MarshalAs(UnmanagedType.Bool)]
      public static extern bool CloseDesktop(IntPtr handle);
  </code></pre>