简体   繁体   English

在C#WebBrowser组件中加载html文件

[英]Load html file in a C# WebBrowser component

In my WPF app I am trying to load an html file(from inside my project, name of html file is index.html) in to a WebBrowser component. 在我的WPF应用程序中,我试图将html文件(从我的项目内部,html文件的名称为index.html)加载到WebBrowser组件中。

I followed an answer byghostJago from this question: Load local HTML file in a C# WebBrowser , but the problem is, the directory of the html file I get points to a Debug folder ( Eg C:\\Users\\user1\\source\\repos\\AppName\\AppName\\bin\\Debug ). 我从这个问题得到了答案byghostJago: 在C#WebBrowser中加载本地HTML文件 ,但是问题是,我得到的html文件目录指向Debug文件夹(例如C:\\ Users \\ user1 \\ source \\ repos \\ AppName \\ AppName \\ bin \\ Debug )。

The first time I ran my project in debug mode, the html file wasn't in the debug folder, I had to paste it in there. 第一次以调试模式运行项目时,html文件不在调试文件夹中,因此我不得不将其粘贴到其中。 So is it possible to load a html file from within visual studio project at runtime? 那么有可能在运行时从Visual Studio项目中加载html文件吗? If so, how do I do it? 如果是这样,我该怎么办?

Code that points to Debug folder: 指向Debug文件夹的代码:

string curDir = Directory.GetCurrentDirectory();
string myFile = System.IO.Path.Combine(curDir, "index.html");
WebView.Navigate("file:///" + myFile);

If you have a file listed in Visual Studios solution explorer, you should be able to right click it, select properties and in the properties pane set the "Copy to Output Directory" to "Copy always" . 如果您在Visual Studios解决方案资源管理器中列出了一个文件,则应该可以右键单击它,选择属性,然后在属性窗格中将“复制到输出目录”设置“始终复制”

The file will then end up next to the exe in the same debug folder and you will be able to refer to it with your existing code. 然后,该文件将在同一调试文件夹中的exe旁边结束,您将可以使用现有代码引用该文件。

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

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