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

C#.NetWinformsUri

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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);

Solution 2 - C#

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://"

Solution 3 - C#

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);
        }
 }

Solution 4 - C#

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://"

Solution 5 - C#

I worked around this by using UriBuilder instead.

UriBuilder builder = new UriBuilder(slct.Text);

if (DeleteFileOnServer(builder.Uri))
{
   ...
}

Solution 6 - C#

The issue for me was that when i got some domain name, i had:

cloudsearch-..-..-xxx.aws.cloudsearch... [WRONG]

http://cloudsearch-..-..-xxx.aws.cloudsearch... [RIGHT]

hope this does the job for you :)

Solution 7 - C#

I was getting a debugging error like this while trying to set up Docker.

I've set the docker setting in "launchSettings.json" as follows.

Problem has solved.

"Docker":
{
	"commandName": "Docker",
	"launchBrowser": true,
	"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/",
	"publishAllPorts": false,
	"useSSL": true
}

Solution 8 - C#

Please check the xml comment file exists or not if you enable SwaggerDoc for the app.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionanon271334View Question on Stackoverflow
Solution 1 - C#CJBrewView Answer on Stackoverflow
Solution 2 - C#SimonView Answer on Stackoverflow
Solution 3 - C#Ashish GuptaView Answer on Stackoverflow
Solution 4 - C#Stephen ClearyView Answer on Stackoverflow
Solution 5 - C#user1023602View Answer on Stackoverflow
Solution 6 - C#WareN The GaMeRView Answer on Stackoverflow
Solution 7 - C#Kemal AkçılView Answer on Stackoverflow
Solution 8 - C#user3724031View Answer on Stackoverflow