[英].ToString Error (vb.net)
For some reason, this is not working for me , and I'm not sure. 由于某种原因,这对我不起作用,我不确定。 I'm trying to make a Set Home Page button for my webbrowser, and this is the code to check if a page is set, and then goto the page:
我正在尝试为我的Web浏览器创建一个“设置主页”按钮,这是检查是否设置了页面,然后转到该页面的代码:
Dim HomepageInfo As String
If IO.File.Exists(Environment.SpecialFolder.ApplicationData & "\Homepage.Info") = True Then
HomepageInfo = IO.File.ReadAllText(Environment.SpecialFolder.ApplicationData & "\Homepage.Info")
WebBrowser1.Url = HomepageInfo.ToString
Else
'Create a File with a Default Homepage (www.google.com)
IO.File.WriteAllText(Environment.SpecialFolder.ApplicationData & "\Homepage.Info", "www.google.com")
End If
And this is showing as an error: HomepageInfo.ToString, and the error is: "Value of type 'String' cannot be converted to 'System.Uri'." 并且这显示为错误:HomepageInfo.ToString,错误是:“类型'String'的值不能转换为'System.Uri'。”
Thanks for any help! 谢谢你的帮助!
WebBrowser.Url
property accepts an uri object not a string: WebBrowser.Url
属性接受uri对象而不是字符串:
Property Value Type: System.Uri A Uri representing the URL of the current document.
属性值类型:System.Uri一个Uri,代表当前文档的URL。
So you have to use an instance of Uri
class: 因此,您必须使用
Uri
类的实例:
Provides an object representation of a uniform resource identifier (URI) and easy access to the parts of the URI.
提供统一资源标识符(URI)的对象表示,并易于访问URI的各个部分。
Code: 码:
Dim HomepageInfo As String
If IO.File.Exists(Environment.SpecialFolder.ApplicationData & "\Homepage.Info") = True Then
HomepageInfo = IO.File.ReadAllText(Environment.SpecialFolder.ApplicationData & "\Homepage.Info")
WebBrowser1.Url = New Uri(HomepageInfo.ToString)
Else
'Create a File with a Default Homepage (www.google.com)
IO.File.WriteAllText(Environment.SpecialFolder.ApplicationData & "\Homepage.Info", "www.google.com")
End If
尝试像这样使用
WebBrowser1.Url = new Uri(HomepageInfo.ToString);
Quite simple: Your WebBrowser1.Url is not a string, but an URL - an URL is NOT a string, but behaves idfferently, eg options for checking he validity of an URL. 非常简单:您的WebBrowser1.Url不是字符串,而是URL-URL不是字符串,但是行为不同,例如,用于检查URL有效性的选项。
You can construct a new URL to circumvent this problem: WebBrowser1.Url = new Uri(HomepageInfo.ToString) 您可以构造一个新的URL来解决此问题:WebBrowser1.Url = new Uri(HomepageInfo.ToString)
But this might fail, of the given string is not a valid URL. 但这可能会失败,因为给定的字符串不是有效的URL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.