title: How to programmatically minimize opened window folders
body: <p>How can I get the list of opened of folders, enumerate through it and minimize each folder programmatically?</p>
  
  <p>At times some opened folders do steal focus from the tool when jumping from one form in the application to another. Preventing this is of high priority for our client. The customers are visually impaired people, so they access the machine only via screen readers. Minimizing other windows (folders) is not at all a problem, in fact a requirement. </p>
  
  <p>I tried this:</p>
  
  <pre><code>foreach (Process p in Process.GetProcessesByName("explorer"))
  {
      p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
  }
  </code></pre>
  
  <p>As expected it did no good.</p>
  
  <p><strong>Update</strong>:</p>
  
  <p>From the answers here, I tried this:</p>
  
  <pre><code>    delegate bool EnumThreadDelegate(IntPtr hWnd, IntPtr lParam);
  
      [DllImport("user32.dll")]
      static extern bool EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn, IntPtr lParam);
  
      static IEnumerable&lt;IntPtr&gt; EnumerateProcessWindowHandles(int processID)
      {
          List&lt;IntPtr&gt; handles = new List&lt;IntPtr&gt;();
  
          EnumThreadDelegate addWindowHandle = delegate(IntPtr hWnd, IntPtr param)
          {
              handles.Add(hWnd);
              return true;
          };
  
          foreach (ProcessThread thread in Process.GetProcessById(processID).Threads)                              
              EnumThreadWindows(thread.Id, addWindowHandle, IntPtr.Zero);
  
          return handles;
      }
  
      const int SW_MINIMIZED = 6;
  
      [DllImport("user32.dll")]
      static extern int ShowWindow(IntPtr hWnd, int nCmdShow);
  
      private void button1_Click(object sender, EventArgs e)
      {
          foreach (IntPtr handle in EnumerateProcessWindowHandles(Process.GetProcessesByName("explorer")[0].Id))
              ShowWindow(handle, SW_MINIMIZED);
      }
  </code></pre>
  
  <p>This creates a whole lot of invisible explorer windows to be suddenly listed in the taksbar out of no where. I am bit noob in dealing with Windows API, so the code itself will actually help.</p>