title: How to check that a uri string is valid
body: <p>How do you check that a uri string is valid (that you can feed it to the Uri constructor)?</p>
  
  <p>So far I only have the following but for obvious reasons I'd prefer a less brute way:</p>
  
  <pre><code>    Boolean IsValidUri(String uri)
      {
          try
          {
              new Uri(uri);
              return true;
          }
          catch
          {
              return false;
          }
      }
  </code></pre>
  
  <p>I tried Uri.IsWellFormedUriString but it doesn't seem to like everything that you can throw at the constructor. For example:</p>
  
  <pre><code>String test = @"C:\File.txt";
  Console.WriteLine("Uri.IsWellFormedUriString says: {0}", Uri.IsWellFormedUriString(test, UriKind.RelativeOrAbsolute));
  Console.WriteLine("IsValidUri says: {0}", IsValidUri(test));
  </code></pre>
  
  <p>The output will be:</p>
  
  <pre><code>Uri.IsWellFormedUriString says: False
  IsValidUri says: True
  </code></pre>
  
  <p><strong>Update/Answer</strong></p>
  
  <p>The Uri constructor uses kind Absolute by default. This was causing a discrepancy when I tried using Uri.TryCreate and the constructor. You do get the expected outcome if you match the UriKind for both the constructor and TryCreate.</p>