title: Issue with monitor numbers when docking laptop to multiple monitors
body: <p>I developed an application that I can assign programs to specific monitors so that when I dock my laptop, which results in all my open windows being pushed to my main monitor, I can press a global hotkey, <kbd>Alt</kbd>+<kbd>d</kbd>, and all the applications that have been assigned to a monitor get pushed to their assigned monitor. I also have 3 other hotkeys, <kbd>Alt</kbd>+<kbd>1</kbd>, <kbd>Alt</kbd>+<kbd>2</kbd> and <kbd>Alt</kbd>+<kbd>3</kbd>, which move the window in focus to the corresponding monitor. I have noticed that when returning to my desk and docking my laptop while it is sleeping and then logging back into Windows that the monitor working area returned in the code does not match what is shown in the display settings.</p>
  
  <p>Here is the monitor layout that Windows shows. In red is what my code is showing.</p>
  
  <p><a href="https://i.stack.imgur.com/1L9AL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1L9AL.png" alt="Monitor Assignment"></a></p>
  
  <p>If I undock and then dock everything matches back up.</p>
  
  <p>Here is my code for moving the currently focused window to a specific monitor.<br>
  <kbd>Alt</kbd>+<kbd>1</kbd> sends a 0, <kbd>Alt</kbd>+<kbd>2</kbd> sends a 1 and <kbd>Alt</kbd>+<kbd>3</kbd> sends a 2:</p>
  
  <pre><code>[DllImport("user32.dll")]
  static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, SetWindowPosFlags uFlags);
  [DllImport("user32.dll")]
  static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  [DllImport("user32.dll")]
  private static extern IntPtr GetForegroundWindow();
  [DllImport("user32.dll")]
  static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);      
  
  [Flags]
  public enum SpecialWindowHandles
  {
      /// &lt;summary&gt;
      ///     Places the window at the bottom of the Z order. If the hWnd parameter identifies a topmost window, the window loses its topmost status and is placed at the bottom of all other windows.
      /// &lt;/summary&gt;
      HWND_BOTTOM = 1            
  }
  [Flags]
  public enum SetWindowPosFlags : uint
  {
      /// &lt;summary&gt;
      ///     Retains the current size (ignores the cx and cy parameters).
      /// &lt;/summary&gt;
      SWP_NOSIZE = 0x0001,
  
      /// &lt;summary&gt;
      ///     Retains the current Z order (ignores the hWndInsertAfter parameter).
      /// &lt;/summary&gt;
      SWP_NOZORDER = 0x0004
  }
  
  private struct WINDOWPLACEMENT
  {
      public int length;
      public int flags;
      public int showCmd;
      public System.Drawing.Point ptMinPosition;
      public System.Drawing.Point ptMaxPosition;
      public System.Drawing.Rectangle rcNormalPosition;
  }
  
  /// &lt;summary&gt;
  /// Moves the user process retaining start to the provided monitor
  /// &lt;/summary&gt;
  /// &lt;param name="monitor"&gt;&lt;/param&gt;
  public void DockWindow(int monitor)
  {
      var screens = Screen.AllScreens.Count();
      if (monitor == 1 &amp;&amp; screens &lt; 2 || monitor == 2 &amp;&amp; screens &lt; 3) // Prevent moving widnow to monitor that doesn't exist
          return;
      var hwnd = GetForegroundWindow(); // Gets the handle for the focused window
      var screenLocation = Screen.AllScreens[monitor].WorkingArea; // Gets the working area of the windows associated monitor
      var placement = new WINDOWPLACEMENT();
      GetWindowPlacement(hwnd, ref placement); // Gest window placement info
      switch (placement.showCmd)
      {
          case 3: // Maximized
              ShowWindow(hwnd, 9); // Switch to regular window
              SetWindowPos(hwnd, (IntPtr)SpecialWindowHandles.HWND_BOTTOM, screenLocation.X + 100, screenLocation.Y + 100, 0, 0, SetWindowPosFlags.SWP_NOZORDER | SetWindowPosFlags.SWP_NOSIZE); // Move window
              ShowWindow(hwnd, 3); // Maximize window
              break;
          default: // Regular window
              SetWindowPos(hwnd, (IntPtr)SpecialWindowHandles.HWND_BOTTOM, screenLocation.X + 100, screenLocation.Y + 100, 0 ,0, SetWindowPosFlags.SWP_NOZORDER | SetWindowPosFlags.SWP_NOSIZE); // Move window
              break;
      }
  }
  </code></pre>
  
  <p><strong>UPDATE</strong><br>
  I did a little more debugging and <code>Screen.AllScreens</code> returns different arrays depending on the state my computer is in when docking:</p>
  
  <p><a href="https://i.stack.imgur.com/17JE2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/17JE2.png" alt="Check this out."></a></p>
  
  <p>Any ideas why this happens?</p>