Search code examples
c#stringexceptionunicodeuri

C# Problem with Uri for non-ASCII File Names


I want to access this file:


    var myUri = new Uri("سلام.txt");

I need Uri format for some reason and I can't use string. Exception is:

Invalid URI: The format of the URI could not be determined.

This link did not help.


Solution

  • You should specify UriKind.Relative if you don't provide full Uri string.

    var uri = new Uri("سلام.txt", UriKind.Relative);
    // or provide full Uri
    var uri = new Uri("http://foo.bar/سلام.txt");