如何在单击c#应用程序中的按钮时在IE中打开网页。 我的目的是为c#应用程序创建一个Web登录,需要在IE中以指定的宽度和高度打开,并且需要在应用程序中调用一个函数。
答案 0 :(得分:40)
来自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();
}
}
}
答案 1 :(得分:18)
System.Diagnostics.Process.Start("iexplore", "http://example.com");
答案 2 :(得分:5)
有一种方法可以在默认浏览器System.Diagnostics.Process.Start(url);
如果你想专门在IE中打开它,你可能需要创建一个以URL作为参数的新IE进程。
UPD:如果要运行函数,请将GET参数插入url字符串(即http://stackoverflow.com/page?runFunction=1
),并在应用程序代码中检查runFunction参数并根据其值确定您的应用程序是否需要运行该功能。
我认为无法指定新的IE窗口宽度和高度值,您可能需要使用javascript。