简体   繁体   English

WPF - 无法在WebBrowser控件中从本地计算机打开文件

[英]WPF - cannot open a file from local machine in WebBrowser control

I am currently developing a HTML Editor with C#, which has a preview option, but it doesn't Compile... And here is my code: 我目前正在开发一个带有C#的HTML编辑器,它有一个预览选项,但它没有编译......这是我的代码:

        string tempPath = System.IO.Path.GetTempPath();//get TEMP folder location
        tempPath += "htmldev\\";
        if (!Directory.Exists(tempPath))
        {
            Directory.CreateDirectory(tempPath);
        }
        tempPath += "current.html";
        if(File.Exists(tempPath))
        {
            File.Delete(tempPath);//delete the old file
        }
        StreamWriter sr = new StreamWriter(tempPath);
        sr.WriteLine(textHtml.Text);//write the HTML code in the temporary file
        sr.Close();
        previewBrowser.Source = new Uri(tempPath);//When I comment this line my program compiles successfully, and the file is created.

I also tried using the Navigate() method, but it didn't work too. 我也尝试过使用Navigate()方法,但它也没有用。

I didn't get any errors or warnings. 我没有收到任何错误或警告。 edit:If I try to open a website, like google.com it works. 编辑:如果我尝试打开一个网站,比如google.com就可以了。

I believe your XAML doesn't run properly because Source="bing.com/" is not a valid argument for Uri constructor (apparently, your code compiles but doesn't run). 我相信你的XAML运行不正常,因为Source="bing.com/"不是Uri构造函数的有效参数(显然,你的代码编译但不运行)。 Just remove Source and it should run: 只需删除Source运行:

<WebBrowser x:Name="previewBrowser" HorizontalAlignment="Left" 
    Height="593" Margin="651,45,0,0" VerticalAlignment="Top" Width="545"/>

If you really need a non-empty WebBrowser initially, use Source="about:blank" or Source="http://bing.com/" . 如果您最初确实需要非空的WebBrowser ,请使用Source="about:blank"Source="http://bing.com/"

The following compiles and runs just fine. 以下编译并运行得很好。

C# : C#

using System;
using System.IO;
using System.Windows;

namespace WpfWb
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.Loaded += (s, e) =>
            {
                var textHtml = "<html><body><b>Hello</b>, World!</body></html>";

                string tempPath = System.IO.Path.GetTempPath();//get TEMP folder location
                tempPath += "htmldev\\";
                if (!Directory.Exists(tempPath))
                {
                    Directory.CreateDirectory(tempPath);
                }
                tempPath += "current.html";
                if (File.Exists(tempPath))
                {
                    File.Delete(tempPath);//delete the old file
                }
                StreamWriter sr = new StreamWriter(tempPath);
                sr.WriteLine(textHtml);//write the HTML code in the temporary file
                sr.Close();

                previewBrowser.Source = new Uri(tempPath);//When I comment this line my program compiles successfully, and the file is created.
            };
        }
    }
}

XAML : XAML

<Window x:Class="WpfWb.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <WebBrowser x:Name="previewBrowser"/>
</Window>

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

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