Invalid URI: The format of the URI could not be determined
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.
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);
Check possible reasons here: http://msdn.microsoft.com/en-us/library/z6c2z492(v=VS.100).aspx
EDIT:
You need to put the protocol prefix in front the address, i.e. in your case "ftp://"
Better use Uri.IsWellFormedUriString(string uriString, UriKind uriKind)
. http://msdn.microsoft.com/en-us/library/system.uri.iswellformeduristring.aspx
Example :-
if(Uri.IsWellFormedUriString(slct.Text,UriKind.Absolute))
{
Uri uri = new Uri(slct.Text);
if (DeleteFileOnServer(uri))
{
nn.BalloonTipText = slct.Text + " has been deleted.";
nn.ShowBalloonTip(30);
}
}
Sounds like it might be a realative uri. I ran into this problem when doing cross-browser Silverlight; on my blog I mentioned a workaround: pass a "context" uri as the first parameter.
If the uri is realtive, the context uri is used to create a full uri. If the uri is absolute, then the context uri is ignored.
EDIT: You need a "scheme" in the uri, e.g., "ftp://" or "http://"