大网数据广告活动
Web服务器与PHP

Use Visual C# method to start browser

· 52CE 我爱测

Describes how to start the default Internet browser programmatically by using Visual C#. Also provides a code example to illustrate how to do this task.

This article shows how to start the default Internet browser by using Visual C#.

Original product version:   Visual Studio
Original KB number:   305703

Note:

Specify the URL, FTP, or file to open

You can specify a URL, a file, or a File Transfer Protocol (FTP) address. All three of these assignments are valid:

string target= "http://www.microsoft.com";
string target = "ftp://ftp.microsoft.com";
string target = "C:\\Program Files\\Microsoft Visual Studio\\INSTALL.HTM";

Use the Process class Start method to start the browser

The Process class contains a static Start method. Because it's a static method, you can call Start without having an instance of a Process class.

System.Diagnostics.Process.Start(target);

For more information about the Process class, see Process Class.

Provide exception handling

Because you take advantage of the default UseShellExecute property when you call the Start method, you don't have to explicitly query the registry to determine which browser is the default. However, if you use this approach on a computer that doesn't have a browser installed, an exception occurs. This exception must be caught so that the appropriate action can be taken.

This example explicitly trap for an error that's generated when the necessary registry key isn't found and indicates that no browser is installed. Additionally, a general exception handler is provided for other errors that may occur. The try...catch block is demonstrated in the complete code listing.

Complete code sample

string target= "http://www.microsoft.com";
//Use no more than one assignment when you test this code.
//string target = "ftp://ftp.microsoft.com";
//string target = "C:\\Program Files\\Microsoft Visual Studio\\INSTALL.HTM";
try
{
    System.Diagnostics.Process.Start(target);
}
catch (System.ComponentModel.Win32Exception noBrowser)
{
    if (noBrowser.ErrorCode==-2147467259)
    MessageBox.Show(noBrowser.Message);
}
catch (System.Exception other)
{
    MessageBox.Show(other.Message);
}

Troubleshooting

This code is highly dependent on the application-file type associations in the HKEY_CLASSES_ROOT (HKCR) hive of the registry. It can lead to unexpected results and exceptions if the registry is damaged. Additionally, file types and extensions may be associated with applications other than the browser. For example, HTM or HTML files may be associated with Web development software instead of the browser.

Source: Microsoft Learn / IT Pro troubleshooting by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / IT Pro troubleshooting
适用范围
See source applicability · Source date 05/30/2022
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。