2009-11-10 30 views
9

我正在開發一個小程序作爲窗體。在這個表單上,我想要一個鏈接,當用戶點擊鏈接時,一個單獨的IE瀏覽器將打開並顯示發佈數據。如何用C#中的發佈信息打開IE?

原件,我用System.Diagnostics.Process.start()。但是,我無法通過這種呼叫發佈數據。

我搜索了網頁,有人提議使用「微軟網頁瀏覽器控件」,但是,這需要在我的表單中添加這個控件,並在窗體中顯示瀏覽器,但我想單獨打開一個IE瀏覽器。

而我在VB中看到有一個方法爲CreateObject("InternetExplorer.Application"),但我找不到如何在C#中使用它。

那麼,你有什麼建議如何實施?

+0

您可以使用Webbrowser控件的Navigate()方法,並將它完全隱藏在窗體中。 – 2009-11-10 02:12:09

回答

13

在窗體上放置一個Web瀏覽器。它應該有一個默認名稱「webBrowser1」 - 你可以改變,如果你喜歡。將「可見」屬性設置爲「False」。雙擊表單標題欄以在代碼中自動生成加載事件。

呼叫導航方法,其中有這樣的簽名:

void Navigate(string urlString, string targetFrameName, byte[] postData, string additionalHeaders); 

像這樣:

private void Form1_Load(object sender, EventArgs e) 
{ 
    webBrowser1.Navigate("http://www.google.com/", "_blank", Encoding.Default.GetBytes("THIS IS SOME POST DATA"), ""); 
} 

你可以通過你在那裏要的字節數組任何... Encoding.Default.GetBytes ()只是一個傳遞字符串的快捷方式。

訣竅是使用「_blank」作爲目標框架。如果你做的ShellExecute與OPEN上的網址,然後默認Web瀏覽器將催生一個動詞

System.Diagnostics.Process.Start("IExplore.exe", "http://localhost/file.html?foo=bar&baz=duh"); 
+0

我懷疑你可以說嵌入一個控件只是爲了執行Process.Start()可以做的事情是有效的? – slugster 2009-11-10 03:18:53

+3

這實際上是.NET最好的簡單答案。 越「正確」的答案涉及在「InternetExplorer.Application」progID上調用CoCreateInstance,在CCI之後,您可以調用.Navigate2接口並傳遞發佈數據。但是這將涉及一堆COM/PInvoke咕,這個答案整齊地避免。 – EricLaw 2009-12-15 22:12:59

-3

其實,你可以使用的Process.Start發佈與查詢字符串數據。或者,你可以在字符串末尾附加鏈接調用的Internet Explorer(再次使用的ShellExecute)(讓您使用ShellExecute的字符串應該是這樣的:

System.Diagnostics.Process.Start(@"C:\Program Files\Internet Explorer\iexplore.exe", "http://google.com"); 

你雖然談論POST ,你不能做一個帖子,上面的線做一個GET根據網站的設置方式,你可能能夠只是追加對URL的末尾的參數,像這樣:

System.Diagnostics.Process.Start(@"C:\Program Files\Internet Explorer\iexplore.exe", "http://www.google.com/search?q=bing"); 
+5

執行GET,而不是POST – 2009-11-10 02:16:14

3

,並打開鏈接:

+1

是否有隨機的downvoter在沒有解釋的情況下進行投票? – slugster 2009-12-15 23:34:03

+1

增加投票反擊非評論隨機downvoter – ryber 2009-12-16 17:08:34

2

你是肯定會需要使用Process.Start(或使用ProcessInfo)來啓動IE:像這樣:

//打開IE在桌面上一個文件單擊一個WinForm

internal static string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); 

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) 
{ 
    System.Diagnostics.Process.Start("IExplore.exe", desktopPath + "\\someHTMLFile.htm"); 
} 

如果您在本頁面向下滾動的LinkLabel的結果:

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

(進程。啓動FrameWork 3的文檔。0)

您會發現一個用戶提供的使用ProcessInfo控制是否啓動了多個IE實例以及如何將命令行參數傳遞給IE的示例。

本頁面:

http://msdn.microsoft.com/en-us/library/0w4h05yb.aspx

(該文檔的Process.Start爲框架3.5)

顯示您啓動IE瀏覽器,以及如何通過URL文件作爲參數的一個完整的例子。

我並不完全清楚你的消息中的「Post」是什麼意思(我把「Post」與ASP.NET關聯起來),但是你可以用你喜歡的任何東西在臨時位置寫出一個html文件它,然後在使用上述技術啓動IE時傳遞該文件的地址。最好,

+0

「post」他意味着POST方法提交數據到服務器,在HTTP中。請參閱第9.5節:http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html。 yongmei的問題在於Process.Start(url)做事的方式使用了GET方法(那裏的9.3節),它不太安全。 – urig 2010-01-06 09:34:06

1

您可以通過發送Process.Start中的URL作爲參數來啓動進程。由於同步上下文,從WebForms GUI線程調用StartProcess時出現問題。我的解決方案爲此使用線程池。這種解決方案的優點是,一個URL被用戶優選的網絡瀏覽器打開,可以是IE,火狐等

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", 
    "CA1725:ParameterNamesShouldMatchBaseDeclaration", MessageId = "0#"), 
System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")] 
public void OpenUrl(string urlString) 
{ 
    try 
    { 
    ThreadPool.QueueUserWorkItem(delegate { StartProcess(urlString, null); }); 
    } 
    catch (Exception ex) 
    { 
    log.Error("Exception during opening Url (thread staring): ", ex); 
    //do nothing 
    } 
} 

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")] 
public void StartProcess(string processName, string arguments) 
{ 
    try 
    { 
    Process process = new Process(); 
    process.StartInfo.FileName = processName; 
    if (!string.IsNullOrEmpty(arguments)) 
    { 
     process.StartInfo.Arguments = arguments; 
    } 
    process.StartInfo.CreateNoWindow = false; 
    process.StartInfo.UseShellExecute = true; 
    process.Start(); 
    } 
    catch (Exception ex) 
    { 
    log.ErrorFormat("Exception in StartProcess: process: [{0}], argument:[{1}], exception:{2}" 
        , processName, arguments, ex); 
    } 
} 
1

還可以使用.NET反射以打開瀏覽器

此示例顯示你如何設置InternetExplorer.Application的一些特定屬性

例如,我需要能夠關閉地址欄並設置高度和寬度。 IE和其他瀏覽器安全不允許您關閉其他示例中的地址欄

我們的網站是內部MVC應用程序,並且沒有任何問題。

System.Type oType = System.Type.GetTypeFromProgID("InternetExplorer.Application"); 
object IE = System.Activator.CreateInstance(oType); 

IE.GetType().InvokeMember("menubar", System.Reflection.BindingFlags.SetProperty, null, IE, new object[] { 0 }); 
IE.GetType().InvokeMember("toolbar", System.Reflection.BindingFlags.SetProperty, null, IE, new object[] { 0 }); 
IE.GetType().InvokeMember("statusBar", System.Reflection.BindingFlags.SetProperty, null, IE, new object[] { 0 }); 
IE.GetType().InvokeMember("addressbar", System.Reflection.BindingFlags.SetProperty, null, IE, new object[] { 0 }); 
IE.GetType().InvokeMember("Visible", System.Reflection.BindingFlags.SetProperty, null, IE, new object[] { true }); 
IE.GetType().InvokeMember("Height", System.Reflection.BindingFlags.SetProperty, null, IE, new object[] { 680 }); 
IE.GetType().InvokeMember("Width", System.Reflection.BindingFlags.SetProperty, null, IE, new object[] { 1030 }); 
IE.GetType().InvokeMember("Navigate", System.Reflection.BindingFlags.InvokeMethod, null, IE, new object[] { "http://yoursite" }); 

這裏唯一的缺點是,這是專門打開IE。優點是它使您能夠更好地控制瀏覽器。

您還可以訪問InternetExplorer.Application對象的事件,方法和屬性。

https://msdn.microsoft.com/en-us/library/aa752084(v=vs.85).aspx

我希望幫助別人,因爲它沒有我。

我正在處理綁定事件,並會在測試後更新它。