使用c#在IE中打开一个网页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1431115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
open a webpage in IE using c#
提问by raki
How to open a webpage in IE while clicking a button in a c# application. My intention is to create a web login for a c# application which need to be opened in IE , in specified width and height, and needs to call a function in the application program.
单击 ac# 应用程序中的按钮时如何在 IE 中打开网页。我的目的是为需要在 IE 中打开的 ac# 应用程序创建一个 web 登录,指定宽度和高度,并需要在应用程序中调用一个函数。
采纳答案by Tzury Bar Yochay
from http://msdn.microsoft.com/en-us/library/system.diagnostics.process(VS.71).aspx
来自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();
}
}
}
回答by RaYell
System.Diagnostics.Process.Start("iexplore", "http://example.com");
回答by Maksim Vi.
There is a way to open a page in default browser System.Diagnostics.Process.Start(url);
有一种方法可以在默认浏览器中打开页面 System.Diagnostics.Process.Start(url);
If you want specifically to open it in IE you will probably need to create a new IE process with URL as an argument.
如果您想专门在 IE 中打开它,您可能需要创建一个以 URL 作为参数的新 IE 进程。
UPD:If you want to run a function, insert GET parameters into your url string (ie. http://stackoverflow.com/page?runFunction=1
) and in your application code check for runFunction parameter and based on its value decide if your application needs to run the function.
UPD:如果您想运行一个函数,请将 GET 参数插入您的 url 字符串(即http://stackoverflow.com/page?runFunction=1
)并在您的应用程序代码中检查 runFunction 参数并根据其值决定您的应用程序是否需要运行该函数。
I don't think is possible to specify new IE window width and height values, you may need to use javascript for that.
我认为不可能指定新的 IE 窗口宽度和高度值,您可能需要为此使用 javascript。