简体   繁体   English

c#webbrowser编码以本地导航到html

[英]c# webbrowser coding to navigate local to html

I'm trying to put a local html location address to the web browser in my C# application but always failed. 我正在尝试在C#应用程序中将本地html位置地址放置到Web浏览器中,但始终失败。 I'm using debug mode now so the html files had already copied into my Debug folder because i put copy always in the copy to output option. 我现在使用调试模式,因此html文件已经复制到了Debug文件夹中,因为我总是将copy放在copy to output选项中。 Below is my code: 下面是我的代码:

        string appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
        string filePath = Path.Combine(appPath, "index.html");
        webBrowser1.Navigate(new System.Uri(@"file://"+ filePath));

There always this error coming out using that way: 总是使用这种方式出现此错误:

An unhandled exception of type 'System.UriFormatException' occurred in System.dll

Any idea what went wrong? 知道出了什么问题吗?

Its a bit unclear but try this out. 尚不清楚,但请尝试一下。 Hope this may help. 希望这会有所帮助。

webBrowser1.Navigate(new Uri(@"your File Name"))

Or 要么

webBrowser1.Navigate(new Uri(AppDomain.CurrentDomain.BaseDirectory == "\\File Name")

Edit: May be this causing error, 编辑:可能是造成错误的原因,

webBrowser1.Navigate(new System.Uri("@" filePath + "file://"));

You just need to change your code slightly - here is the correct syntax: 您只需要稍微更改代码-这是正确的语法:

string appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
string filePath = Path.Combine(appPath, "index.html");
webBrowser1.Navigate(new System.Uri(filePath));

The issue is that CodeBase returns the path in URI format already so you do not need to add the additional the "file://" to the path. 问题在于CodeBase已经以URI格式返回了路径,因此您无需在路径中添加其他“ file://”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM