System.Windows.Controls.WebBrowser导航到磁盘上的html文件

时间:2013-03-26 09:37:36

标签: c# .net visual-studio visual-studio-lightswitch

我觉得自己真的很蠢,但我无法想象这一点。

这完美无瑕;

((System.Windows.Controls.WebBrowser)e.Control).Navigate(new Uri("http://www.google.com"));

但是当我尝试导航到磁盘上的文件时,它失败了

string path =@"D:\dev\MySite.html";

((System.Windows.Controls.WebBrowser)e.Control).Navigate(new Uri(path));

我想我不能使用Uri,但我应该用什么来导航到磁盘上的文件?

完整代码;

    private void webControlAvailable(object sender, ControlAvailableEventArgs e)
    {

         string path =@"D:\dev\MySite.html";

        ((System.Windows.Controls.WebBrowser)e.Control).Navigate(new Uri(path));

    }

1 个答案:

答案 0 :(得分:0)

使用System.Windows.Controls.WebBrowser导航到本地文件非常困难。不过你可以试试这个:

string path =@"file://127.0.0.1/D$/dev/MySite.html";
((System.Windows.Controls.WebBrowser)e.Control).Navigate(new Uri(path));

如果你的网站有JavaScript,那可能会有点难看并失败。

您还可以使用System.IO.Stream进行导航,使用WebBrwoser.NavigateToStream(),查看SO上的this主题和official文档。