简体   繁体   English

Visual Studio表单:在Visual Studio Webbrowser中添加本地index.html文件

[英]Visual studio forms : Add local index.html file in visual studio webbrowser

I am working on a visual studio project in which I have a webpage I would like to display in webbrowser. 我正在一个Visual Studio项目中工作,在该项目中有一个我想在Web浏览器中显示的网页。 Currently I have a folder named resources in which I have copied all the required html,js and css files, but I don't know how to point it in the project. 当前,我有一个名为resources的文件夹,在其中已复制了所有必需的html,js和css文件,但是我不知道如何在项目中将其指向。 Any ideas? 有任何想法吗? Thank you. 谢谢。

Code : 代码:

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);
        }
    }
}

Thank you. 谢谢。

Update 更新资料

I have pasted the contents of the resources folder directly in the project, and tried multiple options suggested in the URL. 我已经直接在项目中粘贴了资源文件夹的内容,并尝试了URL中建议的多个选项。 Unfortunately, nothing is working, and I am new to Visual studio, so don't even have an idea what's going wrong. 不幸的是,什么都没有起作用,而且我是Visual Studio的新手,所以甚至不知道出了什么问题。 Updated code : 更新的代码:

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);
        }

If you want to open a link in a browser from your desktop app (eg "show in the browser" button in WinForms app), you can use System.Diagnostics.Process.Start("http://localhost:42") as it's discussed here (production code will have some non-localhost link there). 如果要从桌面应用程序在浏览器中打开链接(例如WinForms应用程序中的“在浏览器中显示”按钮),则可以使用System.Diagnostics.Process.Start("http://localhost:42")作为在此处进行了讨论(生产代码将在其中具有一些非localhost链接)。

Before that you should host your website on local IIS and specify the port (eg 42) there. 在此之前,您应该将您的网站托管在本地IIS上,并在那里指定端口(例如42)。

On the other hand, if you want to create a web application (with C# back-end code), you should use another type of project (not WinForms), eg ASP.NET MVC 另一方面,如果要创建Web应用程序(带有C#后端代码),则应使用其他类型的项目(而不是WinForms),例如ASP.NET MVC。

EDIT: System.Diagnostics.Process.Start also works with local files, eg System.Diagnostics.Process.Start(@"C:\\Sites\\index.html"); 编辑: System.Diagnostics.Process.Start也可以与本地文件一起使用,例如System.Diagnostics.Process.Start(@"C:\\Sites\\index.html");

EDIT 2: You also could use application directory eg System.Diagnostics.Process.Start(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"site\\index.html")) to keep paths in your app relative 编辑2:您还可以使用应用程序目录,例如System.Diagnostics.Process.Start(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"site\\index.html"))来保持应用程序中相对路径

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

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