Decaying Code

Where code comes to die

About the author

Maxime Rouiller is a passionate .NET technology specialist, working for 7 years in large software development, advocating Agile and TDD. Aware of the latest technological trends, he intervenes as a specialist in the .NET Montréal usergroup and acts regularly as a speaker for Web Form programmers on the MVC platform.

Month List

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.

//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!


Categories: c# | code snippet
Permalink | Comments (0) | Post RSSRSS comment feed
Comments are closed