How to programmatically start the default browser in Visual C # 2005

Source: Internet
Author: User
Tags exception handling ftp


Figure 14-6 shows the execution screen of the program example Ch14_demoform006.cs. We found that when you click a button on a form, you start the default browser and connect to the specified URL. The related program code is written in the Click event handler for the button, as follows:



Figure 14-6



Use the Start method of the Process class to start the default browser and specify the URL, FTP, or file you want to open. Because the Start method is a shared method, you do not need to create an instance of the process class to invoke the Start method. For example, the following is the correct wording:



string target = "http://liminzhang.cnblogs.com/";
System.Diagnostics.Process.Start(target); or string target = "ftp:// ftp.microsoft.com";
System.Diagnostics.Process.Start(target);or string target = @"C:\Program Files\Microsoft
Visual Studio .NET 2005\readme.htm";
System.Diagnostics.Process.Start(target);



When you call the Start method, you take advantage of the default UseShellExecute property, so you do not need an explicit query login (Registry) to verify which browser is the default browser. However, if you use this method on a computer that does not have a default browser installed, an exception will be thrown. You should try to intercept this exception in order to take the appropriate path. For this reason, this example uses a try ... Catch blocks to intercept errors when the required login password is not found. In addition, a general exception handling function is provided to intercept other possible errors.



Try ... The complete program code for the Catch block is listed as follows:



try
{
System.Diagnostics.Process.Start(target);
}
catch (System.ComponentModel.Win32Exception noBrowser)
{
MessageBox.Show(noBrowser.Message);
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.