无效的URI:无法确定URI的格式

105 .net c# uri winforms

我一直收到这个错误.

无效的URI:无法确定URI的格式.

我的代码是:

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

更新: slct.Text中的内容是ftp.jt-software.net/style.css.

是什么赋予了?那怎么不是有效的URI格式?这是纯文本.

CJB*_*rew 112

为Uri使用不同的构造函数可能会有所帮助.

如果您有服务器名称

string server = "http://www.myserver.com";
Run Code Online (Sandbox Code Playgroud)

并有一个相对的Uri路径附加到它,例如

string relativePath = "sites/files/images/picture.png"
Run Code Online (Sandbox Code Playgroud)

当从这两个创建Uri时,我得到"格式无法确定"异常,除非我使用带有UriKind参数的构造函数,即

// 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);
Run Code Online (Sandbox Code Playgroud)


Sim*_*mon 56

请查看可能的原因:http://msdn.microsoft.com/en-us/library/z6c2z492(v = VS.100).aspx

编辑:

您需要将协议前缀放在地址前面,即在您的情况下"ftp://"

  • 没有错.我很快就做了一切,我意外地点击了向下按钮,它说除非问题被编辑,否则我无法撤消它,所以如果你可以善意地编辑你的问题,我可以重新投票给你:)很抱歉关于那个:-(我不是故意要贬低你 (3认同)
  • @Simon,我刚刚再次投票给你,对巨大的延迟感到抱歉!你可以打我两下 哈哈 (2认同)

Ste*_*ary 13

听起来它可能是一个灾难性的uri.我在进行跨浏览器Silverlight时遇到了这个问题.在我的博客上,我提到了一种解决方法:将"context"uri作为第一个参数传递.

如果uri是实际的,则使用上下文uri来创建完整的uri.如果uri是绝对的,则忽略上下文uri.

编辑:您需要在uri中使用"方案",例如"ftp://"或"http://"


Ash*_*pta 12

更好用Uri.IsWellFormedUriString(string uriString, UriKind uriKind).http://msdn.microsoft.com/en-us/library/system.uri.iswellformeduristring.aspx

示例: -

 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);
        }
 }
Run Code Online (Sandbox Code Playgroud)

  • 这没有提供解决方案。它只是检查URI是否正确,如果正确,它将继续。 (3认同)

小智 7

我通过使用UriBuilder来解决这个问题.

UriBuilder builder = new UriBuilder(slct.Text);

if (DeleteFileOnServer(builder.Uri))
{
   ...
}
Run Code Online (Sandbox Code Playgroud)


小智 5

对我来说,问题是当我获得一些域名时,我有:

cloudsearch-..-..-xxx.aws.cloudsearch... [错误]

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

希望这对你有用:)