我试图在我的C#应用程序中将本地html位置地址放到Web浏览器中但总是失败。我现在正在使用调试模式,因此html文件已经复制到我的Debug文件夹中,因为我总是将copy放在copy to output选项中。 以下是我的代码:
string appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
string filePath = Path.Combine(appPath, "index.html");
webBrowser1.Navigate(new System.Uri(@"file://"+ filePath));
总是会出现这种错误:
An unhandled exception of type 'System.UriFormatException' occurred in System.dll
知道出了什么问题吗?
答案 0 :(得分:0)
有点不清楚,但试试这个。希望这可能有所帮助。
webBrowser1.Navigate(new Uri(@"your File Name"))
或者
webBrowser1.Navigate(new Uri(AppDomain.CurrentDomain.BaseDirectory == "\\File Name")
编辑: 可能是这导致错误,
webBrowser1.Navigate(new System.Uri("@" filePath + "file://"));
答案 1 :(得分:0)
您只需稍微更改一下代码 - 这是正确的语法:
string appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
string filePath = Path.Combine(appPath, "index.html");
webBrowser1.Navigate(new System.Uri(filePath));
问题是CodeBase已经以URI
格式返回路径,因此您无需添加额外的" file://"走的路。