HOW TO: Use C # to programmatically start the default Internet browser (from MSDN)

xiaoxiao2021-03-06  70

The release number of this article has been CHS305703

This article discusses a Beta version of Microsoft products. The information in this article is provided as "as", if there is any change without notice.

For this Beta product, Microsoft does not provide formal product support. For information on gain support for Beta versions, see the documentation included in the beta product file or view the site you downloaded.

For Microsoft Visual Basic .NET versions of this article, see

305705.

This task content

summary

Request to specify the URL, FTP or file to be opened, using the START method of the Process class, start the browser provides an exception handling completion code example troubleshooting reference

Summary This article demonstrates how to start the default Internet browser.

Back to top

Claim

Microsoft Visual Studio .NET

Back to top

Specify the URL, FTP, or file you want to open. You can specify a URL, file, or FTP address. The following three 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";

Back to top

Start your browser using the START method of the Process class

The Process class contains a static

START method. Because this is a static method, it is called

Start does not have to be

Example of the Process class.

System.diagnostics.Process.start (Target);

Back to top

Provide abnormal processing because of call

The default is used when the start method

Useshellexecute properties, so you don't have to explicitly query the registry to determine the default browser. However, if this approach is used on a computer that is not installed in a browser, it will have an exception. This exception must be captured to take the appropriate operation. This example explicitly captures errors generated when the required registry key is not found and indicates that the browser is not installed. In addition, a general exception handler is also provided to handle other errors that may occur. The complete code list is displayed

TRY ... CATCH block.

Back to top

Complete code example

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);

}

Back to top

Troubleshooting this code is highly dependent on the registry

The "Application-File Type" associated with the HKEY_CLASSES_ROOT section. This code may cause unexpected results and exceptions if the registry is destroyed. In addition, file types and extensions can be associated with applications other than the browser. For example, an HTM or HTML file may be associated with a web development software instead of a browser. Back to top

Reference

For more information, please refer to the following .NET Framework class library documentation:

http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemdiagnosticsprocessClasStopic.asp

Back to top

The information in this article applies to:

Microsoft Visual C # .NET Beta 2

Recent Updated: 2001-10-25 (1.0) Keyword Kbhowto KbhowTomaster Kbprod2Web KB305703 KBAUDDEVELOPER

转载请注明原文地址:https://www.9cbs.com/read-109044.html

New Post(0)