C# 单击表单上的按钮时打开网站

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9100174/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:56:38  来源:igfitidea点击:

Opening a website when a button on the form is clicked

c#.netwinforms

提问by ykombinator

I am creating a windows application using C#, where in a button on the GUI when clicked, should open a website.

我正在使用 C# 创建一个 Windows 应用程序,单击 GUI 上的按钮时,应打开一个网站。

The web browser will be taken care of by the default settings.

Web 浏览器将由默认设置处理。

I am curious as to how to do that?

我很好奇如何做到这一点?

采纳答案by Krzysztof Cieslinski

If the point is to open a website in your application, you will have to use WebBrowser control. Put WebBrowser control on your form, and add this code to the button which is responsible for opening the site:

如果重点是在您的应用程序中打开一个网站,您将不得不使用 WebBrowser 控件。将 WebBrowser 控件放在表单上,​​并将此代码添加到负责打开站点的按钮:

webBrowser1.Navigate("www.google.com");

回答by stuartd

This will open the specified link in the default web browser:

这将在默认 Web 浏览器中打开指定的链接:

Process.Start("http://www.google.com");

回答by uday

Use WebBrowserclass. The below msdn linkhas the code explained. Hope it helps.

使用WebBrowser类。下面的msdn 链接解释了代码。希望能帮助到你。

回答by Kevin Pope

In Windows8 the Process class has been dropped so I've used the Launcherclass:

在 Windows8 中 Process 类已被删除,因此我使用了Launcher该类:

await Windows.System.Launcher.LaunchUriAsync(new Uri("http://www.google.com"));

You'll also need to set the method to async ( for example private async void myMethod(object params))

您还需要将方法设置为异步(例如private async void myMethod(object params)