2013-10-30 58 views
0

由于某种原因,这不适合我,我不确定。我试图做一个设为首页按钮为我的浏览器,这是检查是否一个页面设置的代码,然后转到这个页面:.ToString错误(vb.net)

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 

,这是表示作为一个错误:HomepageInfo .ToString,错误是:“类型'字符串'的值不能转换为'System.Uri'。”

感谢您的帮助!

+0

如何声明WebBrowser1? – bendataclear

回答

0

WebBrowser.Url属性接受一个URI对象不是字符串:

Property Value Type: System.Uri A Uri representing the URL of the current document.

所以,你必须使用Uri类的一个实例:

Provides an object representation of a uniform resource identifier (URI) and easy access to the parts of the URI.

代码:

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 
0

尝试有像这样使用

WebBrowser1.Url = new Uri(HomepageInfo.ToString); 
0

非常简单:您的WebBrowser1.Url不是一个字符串,而是一个URL - 一个URL不是一个字符串,但行为不正常。用于检查URL的有效性的选项。

你可以构造一个新的URL来绕过这个问题: WebBrowser1.Url =新的URI(HomepageInfo.ToString)

但这可能会失败,给定字符串不是一个有效的URL。