0

I am working on a visual studio project in which I have a webpage I would like to display in webbrowser. 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. 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. Unfortunately, nothing is working, and I am new to Visual studio, so don't even have an idea what's going wrong. 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);
        }
We are Borg
  • 5,117
  • 17
  • 102
  • 225
  • 2
    Possible duplicate of [Load local HTML file in a C# WebBrowser](https://stackoverflow.com/questions/7194851/load-local-html-file-in-a-c-sharp-webbrowser) – Sinatr Dec 13 '17 at 12:59
  • @Sinatr : I have tried the solution in link you gave, but I only see a white page. There is index.html file present directly in the project. Can you please check the updated code. Thank you. :-) – We are Borg Dec 13 '17 at 13:13
  • What is content of `index.html` (could it be blank)? What value `myFile` has and where is `index.html` actually located (windows explorer path). – Sinatr Dec 13 '17 at 13:16
  • @Sinatr : It has html code in it, when I open it with browser, I can see it, but not in the application. Do I have to specify something in the URL part for webbrowser in Form.cs file? – We are Borg Dec 13 '17 at 13:18

1 Answers1

0

If you want to open a link in a browser from your desktop app (e.g. "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).

Before that you should host your website on local IIS and specify the port (e.g. 42) there.

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), e.g. ASP.NET MVC

EDIT: System.Diagnostics.Process.Start also works with local files, e.g. System.Diagnostics.Process.Start(@"C:\Sites\index.html");

EDIT 2: You also could use application directory e.g. System.Diagnostics.Process.Start(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"site\index.html")) to keep paths in your app relative

Dmitry Karpenko
  • 544
  • 5
  • 6
  • We are more interested in creating a packaged application which can be run when an exe is executed without any server and without internet. That's why I am using Visual studio in the first place, or else I can easily do this with other platforms. Any idea what am I doing wrong? Thank you. – We are Borg Dec 13 '17 at 13:24
  • Anyway I can put the files inside the project and work with relative path? Not absolute as we want to package it later. Thank you. – We are Borg Dec 13 '17 at 13:36
  • Do you want to call a static page (e.g. index.html) from your desktop application? If yes, you can open a static page as I've described after EDIT. The static page can be stored in the application folder and called by a relative path. – Dmitry Karpenko Dec 13 '17 at 13:40
  • "I can put the files inside the project and work with relative" - that depends on how it's more convenient for you - if those files are designed for the solution exclusively - you probably should include them to the solution and deploy (install) to some internal app's folder. The concrete path could be put to the app.config in order to change it in production app.config's version if required. – Dmitry Karpenko Dec 13 '17 at 13:44
  • Thank you for the edit, I saw the edit. There are 2 issues if you can answer : 1) Can the ASP.net application be packaged as a exe and executed on machines without internet? 2) Is it possible to use relative path instead of absolute like you have used with C:...., I would like to put all of the html,js,css files in the project itself. – We are Borg Dec 13 '17 at 13:44
  • ASP.NET apps are meant to be deployed to the server (e.g. IIS). They could be [self-hosted](https://blog.uship.com/shippingcode/self-hosting-a-net-api-choosing-between-owin-with-asp-net-web-api-and-asp-net-core-mvc-1-0/) (launched without a server), but I'm not sure whether you want exactly that. Seems like your use cases are not clear. – Dmitry Karpenko Dec 13 '17 at 13:58