【发布时间】:2018-05-27 09:01:47
【问题描述】:
我正在从事一个 Visual Studio 项目,其中我有一个想要在 webbrowser 中显示的网页。目前我有一个名为 resources 的文件夹,我在其中复制了所有必需的 html、js 和 css 文件,但我不知道如何在项目中指向它。有任何想法吗?谢谢。
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
string appPath = Assembly.GetEntryAssembly().Location;
string filename = Path.Combine(Path.GetDirectoryName(appPath), "resources\\index.html");
notes.Navigate(filename);
}
}
}
谢谢。
更新
我已将资源文件夹的内容直接粘贴到项目中,并尝试了 URL 中建议的多个选项。不幸的是,没有任何效果,而且我是 Visual Studio 的新手,所以甚至不知道出了什么问题。更新代码:
private void Form1_Load(object sender, EventArgs e)
{
//string curDir = Directory.GetCurrentDirectory();
//this.notes.Url = new Uri(String.Format("file:///resources/index.html", curDir));
string applicationDirectory = Path.GetDirectoryName(Application.ExecutablePath);
string myFile = Path.Combine(applicationDirectory, "index.html");
notes.Url = new Uri("file:///" + myFile);
}
【问题讨论】:
-
@Sinatr :我已经尝试了您提供的链接中的解决方案,但我只看到一个白页。项目中直接存在 index.html 文件。你能检查更新的代码吗?谢谢你。 :-)
-
index.html的内容是什么(可以为空)?myFile的值是多少,index.html的实际位置在哪里(Windows 资源管理器路径)。 -
@Sinatr : 里面有 html 代码,当我用浏览器打开它时,我可以看到它,但在应用程序中看不到。我是否必须在 Form.cs 文件中的 webbrowser 的 URL 部分中指定某些内容?
标签: c# winforms visual-studio visual-studio-2010 visual-studio-2012