使用C#在IE中打开网页

25

如何在C#应用程序中点击按钮时在IE中打开一个网页。 我的意图是为C#应用程序创建一个Web登录,需要在指定的宽度和高度中在IE中打开,并需要调用应用程序中的一个函数。


1
一个函数,就像在 Web 应用中的 JavaScript 函数一样? - Sasha Chedygov
我所知道的让你的C#代码与客户端的DOM交互的唯一方法是通过将IE嵌入WinForm作为控制器。 否则,你可以编写IE插件来完成这个任务。 如果你只想调用JavaScript函数,你可以使用我下面建议的选项,并在URL中使用参数来实现。 - Tzury Bar Yochay
如果您想进行某种远程身份验证,那么您肯定需要一个 Web 服务吧?您提出的建议似乎有点不寻常(并不是我想要阻止您探究不寻常的事情)。 - Xiaofu
3个回答

44

来自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();

               }    
    }
}

谢谢Tzury,但我已经更新了问题并列出了所有需要的内容,请看一下。 - raki
我所知道的让你的C#代码与客户端的DOM进行交互的唯一方法是通过将IE嵌入WinForm作为控制器。否则,你可以编写自己的IE插件来实现这一点。如果你只想调用JavaScript函数,你可以使用我下面建议的选项,并在URL中使用参数来实现 - Tzury 3分钟前 [删除此评论] - Tzury Bar Yochay

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

5

有一种方法可以在默认浏览器中打开页面System.Diagnostics.Process.Start(url);

如果您想特别在IE中打开它,您可能需要使用URL作为参数创建一个新的IE进程。

更新:如果您想运行一个函数,请将GET参数插入到您的url字符串中(例如:http://stackoverflow.com/page?runFunction=1),并且在您的应用程序代码中检查runFunction参数,并根据其值决定您的应用程序是否需要运行该函数。

我认为不可能指定新的IE窗口宽度和高度值,您可能需要使用javascript来实现。


1
这是正确的答案:使用默认浏览器并避免与系统、安装和自定义相关的许多其他因素,可以使用System.Diagnostics.Process.Start(url)。 - user3074799

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接