【问题标题】:Visual studio forms : Add local index.html file in visual studio webbrowserVisual Studio 表单:在 Visual Studio 网络浏览器中添加本地 index.html 文件
【发布时间】: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


【解决方案1】:

如果您想从您的桌面应用程序在浏览器中打开一个链接(例如 WinForms 应用程序中的“在浏览器中显示”按钮),您可以使用 System.Diagnostics.Process.Start("http://localhost:42") 讨论过的 here(生产代码将有一些非-localhost 链接)。

在此之前,您应该 host your website on local IIS 并在此处指定端口(例如 42)。

另一方面,如果您想创建一个 Web 应用程序(使用 C# 后端代码),您应该使用另一种类型的项目(不是 WinForms),例如ASP.NET MVC

编辑: System.Diagnostics.Process.Start 也适用于本地文件,例如System.Diagnostics.Process.Start(@"C:\Sites\index.html");

编辑 2: 您也可以使用 application directory 例如System.Diagnostics.Process.Start(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"site\index.html")) 保持应用中的路径相对

【讨论】:

  • 我们更感兴趣的是创建一个打包的应用程序,它可以在没有任何服务器和互联网的情况下执行 exe 时运行。这就是我首先使用 Visual Studio 的原因,否则我可以轻松地在其他平台上做到这一点。知道我在做什么错吗?谢谢。
  • 无论如何我可以将文件放入项目中并使用相对路径?不是绝对的,因为我们想稍后打包它。谢谢。
  • 您想从您的桌面应用程序中调用静态页面(例如 index.html)吗?如果是,您可以打开一个静态页面,正如我在 EDIT 之后所描述的那样。静态页面可以存放在应用文件夹中,通过相对路径调用。
  • “我可以将文件放入项目中并与相关文件一起工作” - 这取决于它对您来说更方便 - 如果这些文件是专门为解决方案设计的 - 您可能应该将它们包含在解决方案并部署(安装)到一些内部应用程序的文件夹。如果需要,可以将具体路径放到 app.config 中,以便在生产 app.config 的版本中更改它。
  • 感谢您的编辑,我看到了编辑。如果您能回答,有两个问题: 1) ASP.net 应用程序可以打包为 exe 并在没有互联网的机器上执行吗? 2)是否可以使用相对路径而不是绝对路径,就像您在 C:.. 中使用的那样,我想将所有 html、js、css 文件放在项目本身中。
猜你喜欢
  • 2018-01-17
  • 1970-01-01
  • 1970-01-01
  • 2010-11-18
  • 1970-01-01
  • 2010-11-22
  • 1970-01-01
  • 1970-01-01
  • 2018-04-18
相关资源
最近更新 更多