您的位置:首页 > 编程语言 > C#

C#学习之打开本地浏览器

2015-10-09 06:52 441 查看

-------------引入的名称空间

System.Diagnostics


-------------方法1

      打开IE的方法:

      函数原型:

public static Process Start
(
string fileName,
string arguments
)
     举例:

//调用IE浏览器
System.Diagnostics.Process.Start("iexplore.exe", "https://www.google.com.hk/?gws_rd=cr");

 ----------方法2

      打开本地默认浏览器的方法:

      函数原型:

public static Process Start(
string fileName
)


      举例:

//调用系统默认的浏览器
System.Diagnostics.Process.Start("https://www.google.com.hk/?gws_rd=cr");
       

---------------------代码区

    下面的代码展示了使用Process类启动进程。

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

namespace MyProcessSample
{
class MyProcess
{
public static void Main()
{
Process myProcess = new Process();

try
{
myProcess.StartInfo.UseShellExecute = false;
// You can start any process, HelloWorld is a do-nothing example.
myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
// This code assumes the process you are starting will terminate itself.
// Given that is is started without a window so you cannot terminate it
// on the desktop, it must terminate itself or you can do it programmatically
// from this application using the Kill method.
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}


   下面的代码展示了使用Process类的静态方法Start启动进程。

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

namespace MyProcessSample
{
class MyProcess
{
// Opens the Internet Explorer application.
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);
}

// Opens urls and .html documents using Internet Explorer.
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");
}

// Uses the ProcessStartInfo class to start new processes,
// both in a minimized mode.
void OpenWithStartInfo()
{
ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
startInfo.WindowStyle = ProcessWindowStyle.Minimized;

Process.Start(startInfo);

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

Process.Start(startInfo);
}

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();
}
}
}
        参考文档:有个地方叫msdn
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息