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

anon271334 picture anon271334 · May 22, 2010 · Viewed 340.8k times · Source

I keep getting this error.

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

the code I have is:

Uri uri = new Uri(slct.Text);
if (DeleteFileOnServer(uri))
{
    nn.BalloonTipText = slct.Text + " has been deleted.";
    nn.ShowBalloonTip(30);
}

Update: the content in slct.Text is ftp.jt-software.net/style.css.

What gives? How is that not a valid URI format? It's plain text.

Answer

CJBrew picture CJBrew · Sep 9, 2010

It may help to use a different constructor for Uri.

If you have the server name

string server = "http://www.myserver.com";

and have a relative Uri path to append to it, e.g.

string relativePath = "sites/files/images/picture.png"

When creating a Uri from these two I get the "format could not be determined" exception unless I use the constructor with the UriKind argument, i.e.

// this works, because the protocol is included in the string
Uri serverUri = new Uri(server);

// needs UriKind arg, or UriFormatException is thrown
Uri relativeUri = new Uri(relativePath, UriKind.Relative); 

// Uri(Uri, Uri) is the preferred constructor in this case
Uri fullUri = new Uri(serverUri, relativeUri);