Chris Eather
Chris Eather

Reputation: 3

Write Current URL To String C#

This is what I am trying to do.

  1. Windows Form App loads with web browser
  2. Goes to a certain link
  3. Checks to see if the url bar is empty, if not it takes the current url and writes to a string and then navigates to some other url.

I keep getting Cannot implicitly convert type 'System.Uri' to 'string' and I have tried a few things but can't get my head around it.

string url;

try
{
    if (webBrowser1.Url != null)
    {
        // url = webBrowser1.Url;

        MessageBox.Show("Success!");

    }
    else
    {
        MessageBox.Show(":(!");
    }
}
catch
{
     MessageBox.Show("Something Screwed Up");
}

Now at this point I get :( when I comment out the error. This is on form1.cs - should I be doing this on program.cs? It seems like the object may not be created a the point when I check but I have no idea. By default the form loads with a URL pre-loaded.

Upvotes: 0

Views: 3518

Answers (1)

Nahum
Nahum

Reputation: 7197

webBrowser1.Url is URI object

url is String object

it tells you those are diffrent types and there no implicit convertion

url = webBrowser1.Url.ToString();

see great article:

http://msdn.microsoft.com/en-us/library/ms173105.aspx

Upvotes: 4

Related Questions