2009-09-16 63 views
21

如何在IE中打開一個網頁,同時點擊c#應用程序中的一個按鈕。 我的意圖是創建一個c#應用程序的web登錄,它需要在IE中以指定的寬度和高度打開,並且需要在應用程序中調用一個函數。在IE中使用c打開一個網頁#

+1

Web應用程序中的JavaScript函數中的函數? – 2009-09-16 05:41:54

+0

我知道讓C#代碼與* client *端的DOM交互的唯一方法是將IE作爲控制器嵌入到WinForm中。 否則,你可以編寫你的IE加載項,它會爲你做這個 如果你只想調用一個JavaScript函數,你可以使用我在下面建議的選項(作爲答案),並使用URL中的參數來這樣做 – 2009-09-16 05:53:31

+0

如果你想做某種遠程認證,你肯定需要一個Web服務?你提出的建議似乎有點不尋常(不是我想阻止你調查不尋常的事情)。 – Xiaofu 2009-09-16 05:57:36

回答

39

http://msdn.microsoft.com/en-us/library/system.diagnostics.process(VS.71).aspx

using System; 
using System.Diagnostics; 
using System.ComponentModel; 

namespace MyProcessSample 
{ 
    /// <summary> 
    /// Shell for the sample. 
    /// </summary> 
    public class MyProcess 
    { 

     /// <summary> 
     /// Opens the Internet Explorer application. 
     /// </summary> 
     public void OpenApplication(string myFavoritesPath) 
     { 
      // Start Internet Explorer. Defaults to the home page. 
      Process.Start("IExplore.exe"); 

      // Display the contents of the favorites folder in the browser. 
      Process.Start(myFavoritesPath); 

     } 

     /// <summary> 
     /// Opens urls and .html documents using Internet Explorer. 
     /// </summary> 
     public void OpenWithArguments() 
     { 
      // url's are not considered documents. They can only be opened 
      // by passing them as arguments. 
      Process.Start("IExplore.exe", "www.northwindtraders.com"); 

      // Start a Web page using a browser associated with .html and .asp files. 
      Process.Start("IExplore.exe", "C:\\myPath\\myFile.htm"); 
      Process.Start("IExplore.exe", "C:\\myPath\\myFile.asp"); 
     } 

     /// <summary> 
     /// Uses the ProcessStartInfo class to start new processes, both in a minimized 
     /// mode. 
     /// </summary> 
     public void OpenWithStartInfo() 
     { 

      ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe"); 
      startInfo.WindowStyle = ProcessWindowStyle.Minimized; 

      Process.Start(startInfo); 

      startInfo.Arguments = "www.northwindtraders.com"; 

      Process.Start(startInfo); 

     } 

     public static void Main() 
     { 
        // Get the path that stores favorite links. 
        string myFavoritesPath = 
        Environment.GetFolderPath(Environment.SpecialFolder.Favorites); 

        MyProcess myProcess = new MyProcess(); 

      myProcess.OpenApplication(myFavoritesPath); 
      myProcess.OpenWithArguments(); 
      myProcess.OpenWithStartInfo(); 

       }  
    } 
} 
+0

謝謝你Tzury,但我已經更新了所有我需要的問題plz看一看吧 – raki 2009-09-16 05:36:22

+0

我知道你的C#代碼與客戶端的DOM交互的唯一方法是將IE作爲控制器嵌入到WinForm中。否則,你可以編寫自己的IE加載項來完成這個任務如果你只想調用一個JavaScript函數,你可以使用我在下面建議的選項(作爲答案)並在URL中使用一個參數來實現 - Tzury 3分鐘前[刪除此評論] – 2009-09-16 05:57:31

18
System.Diagnostics.Process.Start("iexplore", "http://example.com"); 
5

有一個在默認瀏覽器打開一個網頁System.Diagnostics.Process.Start(url);

如果你想特別是在IE中打開它,你可能需要創建一個新的IE的方式以URL作爲參數處理。

UPD:(即http://stackoverflow.com/page?runFunction=1如果你想運行功能,插入GET參數到您的URL字符串,並在runFunction參數應用程序代碼檢查,並根據其值決定,如果你的應用程序需要運行功能。

我不認爲可以指定新的IE窗口寬度和高度值,您可能需要使用JavaScript。

+1

這是正確答案:System.Diagnostics.Process.Start(url),使用默認瀏覽器並避免許多與系統,安裝和定製相關的其他因素。 – 2013-12-06 14:24:21