1

I'm new to C# development and don't know a thing about how resources work. I need to include a whole offline website in my project and display it in WebBrowser. I've already copied the folder into my project directory but what URL should I use to reference it?

enter image description here

Is it included correctly or should be somehow marked as a resource?

m93a
  • 8,866
  • 9
  • 40
  • 58
  • You can't do this in a windows form application. I'd create a MVC project and when you run that visual studio will use IIS to make a local web server for you under local host. You can then navigate to it using http:/localhost:port/Website/index.html – Joshua Duxbury Apr 17 '17 at 15:34
  • Sounds a lot like an overkill... I just need to display the static html files. – m93a Apr 17 '17 at 15:37
  • 1
    You could just open the file with the file location. C:/../index.html without creating a localhost website maybe? – Joshua Duxbury Apr 17 '17 at 15:40

1 Answers1

1

You could just open the directly using it's whole path. This can be done using this code:

var path = Path.Combine(Environment.CurrentDirectory, @"Website\index.html");
Browser.Navigate(path);

It takes the current directory of your program and appends the relative path of your file to it. Then it passes the resulting absolute URI to the WebBrowser.

Note however that this will work only if you change the properties of the folder so that all files inside will be marked as Copy to Output Directory. First you select all the files – only files, not folders! – in the Solution Explorer, then in Properties find Copy to Output Directory and set it either to Copy always or Copy if newer. This will ensure that all the files get copied to the bin folder, from which your program gets executed.

Community
  • 1
  • 1
Joshua Duxbury
  • 4,892
  • 4
  • 32
  • 51
  • Sure I could, but the compiled code gets copied into `bin/Debug`, which means I can't use `Environment.CurrentDirectory`. I could do `\..\..` but that doesn't *feel* like the right thing to do considering portability. Also the resulting program would be unusable in real-case scenario. – m93a Apr 17 '17 at 16:00
  • If you set it as a resource it will get built in the bin folder with the rest of the code. You can then access it via Environment.CurrentDirectory – Joshua Duxbury Apr 17 '17 at 16:02
  • But how do I mark a whole folder as a resource? The html files have relative links to images and scripts... – m93a Apr 17 '17 at 16:05
  • 1
    You can just mark the folder as copy to output directory http://stackoverflow.com/a/7358644 – Joshua Duxbury Apr 17 '17 at 16:16
  • This is the answer I needed ;) – m93a Apr 17 '17 at 18:00