Code Snippet - Quickly changing the host in an URL in C#

Quick code snippet today. Let’s say you have some URLs that you want to change the hostname/port on it without having to do string manipulation.

1
2
3
4
5
6
7
//multiple constructor are available
UriBuilder builder = new UriBuilder("http://localhost:1712")
{
Host = Environment.MachineName,
Port = 80
};
Uri myNewUri = builder.Uri;

There you go! It’s as easy as this. This will avoid countless hour of parsing a URL for the hostname, port number or whatever else you are searching.

The bonus with that is that the path of the URL won’t be touched at all.

Have fun!