简体   繁体   English

在webBrowser中添加HTML文件

[英]Add HTML file in webBrowser

I'm a newbie with this. 我是这个新手。 Here is my problem: 这是我的问题:

I want to call a HTML file located in my PC clicking a button like this (I'm using c#). 我想通过单击这样的按钮来调用位于我的PC中的HTML文件(我正在使用c#)。

private void Button1_click(object sender, RoutedEventArgs e) 
{
 navegador.Navigate("C:/Users/aMan/Desktop/HelloWorld.html");
}

In which navegador is the name of the webBrowser control . 其中的navegadorwebBrowser control的名称。

I was reading some notes that you answered before, even following the steps I can't get my webBrowser loads that file. 我正在阅读一些您之前回答的笔记,即使按照以下步骤操作,我也无法让我的webBrowser加载该文件。 So pretty please, with sugar on top, help me out. 太漂亮了,上面放糖,帮帮我。

I believe, correct me if I am wrong but you need the directory to be double slashed in order for it to work. 我相信,如果我错了,请纠正我,但您需要将目录双斜杠以使其正常工作。

Uri HTMLPath = new Uri("C://Users//aMan//Desktop//HelloWorld.html");
navegador.Navigate(HTMLPath);

How about that? 那个怎么样?

Just add an @ before the string like so. 只需在字符串之前添加一个@即可。

navegador.Navigate(@"C:/Users/aMan/Desktop/HelloWorld.html");

Your basic command is correct. 您的基本命令是正确的。 It is just the either the @ or // slashes are required in strings so that .NET does not treat single slashes (/) as escape characters. 它只是字符串中必须使用@或//斜杠,因此.NET不会将单个斜杠(/)视为转义字符。

Also, (and this is just a bonus), the WebBrowser control does not function well with modern browsers and is not the most optimal way to retrieve Web content for a page. 另外,(这只是一个奖励),WebBrowser控件在现代浏览器中不能很好地工作,也不是检索页面Web内容的最佳方法。

Vb.net: Vb.net:

        Dim theFileHtml As String = System.IO.File.ReadAllText("c:\test1.txt")
        WebBrowser1.Navigate("")
        Do Until WebBrowser1.ReadyState = WebBrowserReadyState.Complete
            Application.DoEvents()
            System.Threading.Thread.Sleep(1)
        Loop
        WebBrowser1.Document.Body.InnerHtml = theFileHtml 'Also You Can Use WebBrowser1.DocumentText

C#: C#:

string theFileHtml = System.IO.File.ReadAllText("c:\\test1.txt");
    WebBrowser1.Navigate("");
    while (WebBrowser1.ReadyState != WebBrowserReadyState.Complete) {
        Application.DoEvents();
        System.Threading.Thread.Sleep(1);
    }
        //Also You Can Use WebBrowser1.DocumentText
    WebBrowser1.Document.Body.InnerHtml = theFileHtml;

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

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