Ben
Ben

Reputation: 15

How do I create a link using C#?

Could anyone tell me how to create a link from a LinkLabel in Visual Studio?

Say I'm trying to make the program pull up a browser window to www.google.com (in their default browser). How would I do that? I got the following from some example code I found:

        HttpWebRequest head_request = (HttpWebRequest)WebRequest.Create("http://www.google.com");
        head_request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0a2) Gecko/20110613 Firefox/6.0a2";
        HttpWebResponse response = (HttpWebResponse)head_request.GetResponse();

But what I have doesn't do anything. If anything, it makes my browser go into a state of unresponsiveness.

I have

using System.Net;
using System.IO;

up top. Is that right? Thanks in advance!

Upvotes: 1

Views: 9025

Answers (2)

Tarik
Tarik

Reputation: 81711

What you can do like something below:

ProcessStartInfo sInfo = new ProcessStartInfo("http://mysite.com/");  
Process.Start(sInfo);

An article about it: http://support.microsoft.com/kb/320478

Attach it to a link:

protected void hyperlink_Click(object sender, EventArgs e)
{
   ProcessStartInfo sInfo = new ProcessStartInfo("http://mysite.com/");  
   Process.Start(sInfo);
}

Note: If you can't see it, then you should declare using System.Diagnostics; namespace.

Upvotes: 1

James Hill
James Hill

Reputation: 61793

The code below will open google.com in the default browser. You can call this code from anywhere. The click event of a button would be a good place to test it out!

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

Upvotes: 1

Related Questions