title: Internet Explorer Automation: how to suppress Open/Save dialog?
body: <p><strong>When controlling IE instance via MSHTML, how to suppress Open/Save dialogs for non-HTML content?</strong></p>
  <p>I need to get data from another system and import it into our one. Due to budget constraints no development (e.g. WS) can be done on the other side for some time, so my only option for now is to do web scraping.</p>
  <p>The remote site is ASP.NET-based, so simple HTML requests won't work -- too much JS.</p>
  <p>I wrote a simple C# application that uses MSHTML and SHDocView to control an IE instance. So far so good: I can perform login, navigate to desired page, populate required fields and do submit.</p>
  <p>Then I face a couple of problems:</p>
  <p>First is that report is opening in another window. I suspect I can attach to that window too by enumerating IE windows in the system.</p>
  <p>Second, more troublesome, is that report itself is CSV file, and triggers Open/Save dialog. I'd like to avoid it and make IE save the file into given location OR I'm fine with programmatically clicking dialog buttons too (how?)</p>
  <p>I'm actually totally non-Windows guy (unix/J2EE), and hope someone with better knowledge would give me a hint how to do those tasks.</p>
  <p>Thanks!</p>
  <p><strong>UPDATE</strong></p>
  <p>I've found a promising document on MSDN: <a href="http://msdn.microsoft.com/en-ca/library/aa770041.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-ca/library/aa770041.aspx</a></p>
  <p><em>Control the kinds of content that are downloaded and what the WebBrowser Control does with them once they are downloaded. For example, you can prevent videos from playing, script from running, or new windows from opening when users click on links, or prevent Microsoft ActiveX controls from downloading or executing.</em></p>
  <p>Slowly reading through...</p>
  <p><strong>UPDATE 2: MADE IT WORK, SORT OF...</strong></p>
  <p>Finally I made it work, but in an ugly way. Essentially, I register a handler &quot;before navigate&quot;, then, in the handler, if the URL is matching my target file, I cancel the navigation, but remember the URL, and use WebClient class to access and download that temporal URL directly.</p>
  <p>I cannot copy the whole code here, it contains a lot of garbage, but here are the essential parts:</p>
  <p>Installing handler:</p>
  <pre><code>_IE2.FileDownload += new DWebBrowserEvents2_FileDownloadEventHandler(IE2_FileDownload);
  _IE.BeforeNavigate2 += new DWebBrowserEvents2_BeforeNavigate2EventHandler(IE_OnBeforeNavigate2);
  </code></pre>
  <p>Recording URL and then cancelling download (thus preventing Save dialog to appear):</p>
  <pre><code>public string downloadUrl;
  
  void IE_OnBeforeNavigate2(Object ob1, ref Object URL, ref Object Flags, ref Object Name, ref Object da, ref Object Head, ref bool Cancel)
  {
      Console.WriteLine(&quot;Before Navigate2 &quot;+URL);
  
      if (URL.ToString().EndsWith(&quot;.csv&quot;))
      {
          Console.WriteLine(&quot;CSV file&quot;);
          downloadUrl = URL.ToString();
      }
  
      Cancel = false;
  }
  
  void IE2_FileDownload(bool activeDocument, ref bool cancel)
  {
      Console.WriteLine(&quot;FileDownload, downloading &quot;+downloadUrl+&quot; instead&quot;);
      cancel = true;
  }
  
      void IE_OnNewWindow2(ref Object o, ref bool cancel)
      {
          Console.WriteLine(&quot;OnNewWindow2&quot;);
  
          _IE2 = new SHDocVw.InternetExplorer();
          _IE2.BeforeNavigate2 += new DWebBrowserEvents2_BeforeNavigate2EventHandler(IE_OnBeforeNavigate2);
          _IE2.Visible = true;
          o = _IE2;
  
          _IE2.FileDownload += new DWebBrowserEvents2_FileDownloadEventHandler(IE2_FileDownload);
  
          _IE2.Silent = true;
  
          cancel = false;
          return;
      }
  </code></pre>
  <p>And in the calling code using the found URL for direct download:</p>
  <pre><code>        ...
          driver.ClickButton(&quot;.*_btnRunReport&quot;);
          driver.WaitForComplete();
  
          Thread.Sleep(10000);
  
          WebClient Client = new WebClient();
          Client.DownloadFile(driver.downloadUrl, &quot;C:\\affinity.dump&quot;);
  </code></pre>
  <p>(driver is a simple wrapper over IE instance = _IE)</p>
  <p>Hope that helps someone.</p>