簡體   English   中英

將本地網頁加載到webbrowser控件中

[英]Load a local webpage into the webbrowser control

我試圖將webbrowser控件添加到窗口中,然后打開頁面。 我嘗試使用Web URL以及本地HTML文件都無濟於事。 這是我的代碼:

命名空間qTab1 {公共子類Form1:Form {public Form1(){InitializeComponent(); }

    private void Form1_Load(object sender, EventArgs e)
    {
        FileStream source = new FileStream("index.html", FileMode.Open, FileAccess.Read);
        webBrowser1.DocumentStream = source;
        //// When the form loads, open this web page.
        //webBrowser1.Navigate("www.google.com");
    }

    private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
    {
        // Set text while the page has not yet loaded.
        this.Text = "Navigating";
    }

    private void webBrowser1_DocumentCompleted(object sender,
        WebBrowserDocumentCompletedEventArgs e)
    {
        // Better use the e parameter to get the url.
        // ... This makes the method more generic and reusable.
        this.Text = e.Url.ToString() + " loaded";
    }
}

}

這是我目前的項目:

在此處輸入圖片說明

我究竟做錯了什么?

發生這種情況的原因是,當您按Debug或以任何其他方式構建項目時,根目錄是可執行文件的目錄(因此它將是-./bin/Debug ),而不是項目的目錄。

要解決此問題,您可以執行以下操作:

  1. 右鍵單擊html文件,單擊“ 屬性 ”,然后將變量“ 復制到輸出目錄 ”設置為“ 始終復制 ”。 這樣,html文件將與可執行文件一起復制。
  2. 現在,您必須將本地文件加載到WebBrowser控件中。 以下應該工作:

    webBrowser1.Url =新的Uri(“ file:///” + Directory.GetCurrentDirectory()+“ /index.html”);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM