0

我正在使用Windows服務,該服務使用Internet Explorer上的不同代理從csv文件加載頁面並截取它。Internet Explorer未設置爲可見(c#)

我有問題(除了我不能刪除index.dat,但這是一個不同的故事),是當我想使Internet Explorer可見(IE.Visible = true),它不起作用。有什麼建議麼?

這裏是代碼的一部分。 「WriteLog」函數用於在運行發行版本時檢查發生了什麼(里程碑)。

使用Windows 7時,Internet Explorer 9和Visual Studio 2010

static void testSite (string url, int rowNum) 
{ 
    WebLoaderHelper.WriteLog("TS: Variables"); 
    int width, height, hwnd; 
    StringBuilder sb = new StringBuilder(); 
    Stopwatch chronometer = new Stopwatch(); 

    //Init Variables 
    isDocumentComplete = false; 
    isNavigateError = false; 
    errorCode = 200; 

    long result; 
    InternetExplorer IE = null; 
    IE = new SHDocVw.InternetExplorer(); 
    Stopwatch stopWatch = new Stopwatch(); 
    string proxy; 

    proxy = getProxy(); 

    IE.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(IE_DocumentComplete); 
    IE.NavigateError += new DWebBrowserEvents2_NavigateErrorEventHandler(IE_NavigateError); 


    //Helper.setWindowState(IE.HWND, 2); 
    WebLoaderHelper.WriteLog("TS: IE VISIBLE"); 
    try 
    { 
     IE.Visible = Boolean.Parse(Properties.Settings.Default.IEVisible); 
     WebLoaderHelper.WriteLog("TS: IE VISIBLE:" + IE.Visible.ToString()); 
     IE.Visible = true; 
     WebLoaderHelper.WriteLog("TS: IE VISIBLE (true):" + IE.Visible.ToString()); 
    } 
    catch (Exception e) 
    { 
     WebLoaderHelper.WriteLog("TS: IE VISIBLE EXCEPTION:" + e.ToString()); 
     WebLoaderHelper.WriteLog("TS: IE VISIBLE:" + IE.Visible.ToString()); 
     IE.Visible = true; 
     WebLoaderHelper.WriteLog("TS: IE VISIBLE again:" + IE.Visible.ToString()); 
    } 

    WebLoaderHelper.WriteLog("TS: Watches"); 
    stopWatch.Start(); 
    chronometer.Start(); 
    WebLoaderHelper.WriteLog("TS: Navigate"); 
    IE.Navigate(url.ToString()); 

    //Log Start DateTime 
    ResultsDT.Rows[rowNum][0] = DateTime.Now; 

    WebLoaderHelper.WriteLog("TS: While complete"); 
    while (!isDocumentComplete) 
    { 
     if (chronometer.Elapsed.TotalSeconds > 60) 
     { 
      sb.AppendLine("Proxy: " + proxy); 
      sb.AppendLine("site: " + url); 
      sb.AppendLine("Took: " + chronometer.Elapsed.TotalSeconds.ToString() + "seconds and isn't complete. Breaking while."); 
      sb.AppendLine(); 
      WebLoaderHelper.WriteLog(sb.ToString()); 
      WebLoaderHelper.WriteEToErrorLog(sb.ToString()); 
      sb = new StringBuilder(); 
      chronometer.Stop(); 
      break; 
     } 
    } 


    //Artificially add half a second, to let all images load 
    Thread.Sleep(500); 

    //Take Pic 
    WebLoaderHelper.WriteLog("TS: Take Pic"); 
    if (IE.Visible) 
    { 
     WebLoaderHelper.WriteLog("TS: IE Visible PIC!"); 
     try 
     { 
      width = IE.Width; 
      height = IE.Height; 
      WebLoaderHelper.getScreenShot(IE.HWND, width, height, proxy, url); 
     } 
     catch (Exception e) 
     { 
      sb.AppendLine("Proxy: " + proxy); 
      sb.AppendLine("site: " + url); 
      sb.AppendLine(e.Message); 
      sb.AppendLine(); 

      WebLoaderHelper.WriteEToErrorLog(sb.ToString()); 
      WebLoaderHelper.WriteLog("TS: IE VISIBLE PIC but EXCEPTION: " + e.ToString()); 
     } 
    } 

    WebLoaderHelper.WriteLog("TS: Stop everything"); 

    IE.Stop(); 
    IE.Quit(); 
    stopWatch.Stop(); 
    result = stopWatch.ElapsedMilliseconds; 


    try 
    { 
      cleanProcessList(); 
    } 
    catch (Exception) 
    { 
      Console.WriteLine("Problem with cleaning process list"); 
      WebLoaderHelper.WriteLog("TS: Problem with cleaning process list"); 
    } 


     //Log Result 
     ResultsDT.Rows[rowNum][3] = result; 
     ResultsDT.Rows[rowNum][4] = errorCode; 

}

回答

0

不支持通過服務後臺運行的自動化使用Internet Explorer。我不會感到驚訝的是,它明確拒絕在此配置中可見。

不知道爲什麼你會想要刪除index.dat,但不,你永遠不會做到這一點,也不應該。如果你想清除瀏覽器的緩存,那就有WinINET API。