tags:

views:

1175

answers:

3

This is probably very simple, but I simply cannot find the answer myself :(

Basicaly, what I want is, given this string:

"http://www.google.com/search?hl=en&q=c# objects"

I want this output:

http://www.google.com/search?hl=en&q=c%23+objects

I'm sure there's some helper class somewhere buried in the Framework that takes care of that for me, but I'm having trouble finding it.

EDIT: I should add, that this is for a Winforms App.

+6  A: 

Look at Server.UrlEncode

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

yodaj007
Don't think that would work, I'm working on a Winforms App.
BFree
You should be still able to reference the relevant library to perform that encoding operation.
Perhentian
+4  A: 

HttpServerUtility.UrlEncode(string)

Should sort out any troublesome characters

To use it you'll need to add a reference to System.Web (Project Explorer > References > Add reference > System.Web)

Once you've done that you can use it to encode any items you wish to add to the querystring:

System.Web.HttpUtility.UrlEncode("c# objects");
Wilfred Knievel
Dang 58 seconds too slow, time to breakout Mavis Beacon! ;-)
Wilfred Knievel
Hmm didn't know about this one. :)
Dacto
Thanks Wilfred..I dont dint about this too :)
Shoban
Don't think that would work, I'm working on a Winforms App
BFree
Can you just add a reference to System.Web (Project Explorer > references > Add reference > System.Web), and use System.Web.HttpUtility.UrlEncode("c# objects");
Wilfred Knievel
BFree
+1  A: 

@Wilfred Knievel has the accepted answer, but you could also use Uri.EscapeUriString() if you wanted to avoid the dependency on the System.Web namespace.

dthrasher