Tag Archives: CSharp

How to Validate URL in C#

I searched google for how to validate URL in C# and most results say using a regular expression. Eventually I found Uri.TryCreate which is a built in method in C# to check if a string is a valid URL

Uri uri = null;

if (!Uri.TryCreate(url, UriKind.Absolute, out uri) || null == uri)


    //Invalid URL

    return false;





C# reading from and write to text file

CSharp sample for reading from a text file and writing to a text file.

//Reading from a text file      

System.IO.StreamReader srFile = new System.IO.StreamReader(@"c:foo.txt");

string str = srFile.ReadToEnd();



//Writing to a text file

string lines = "foobar";

System.IO.StreamWriter swFile = new System.IO.StreamWriter(@"c:bar.txt");