1

I have a vb.net app that contains a WebBrowser control. The app loads an html/css/javascript file that requests a csv text file. A csv text file is selected from the local hard drive ("C:...") via a browse button . The data from the csv file is loaded into the WebBrowser and displayed in a table created by the javascript. Finally the html displaly is saved as a new html file in the local drive ("C:...")

I would like to include the html/css/javascript file with other files of the vb.net app, i.e., I want the vb.net to contain the html/css/javascript file. The question is what folder do I put it in and are there any special instructions for calling out the paths to the file?

Ed Jenkins
  • 133
  • 10
  • Does this answer your question? [Where to save application data in .NET application](https://stackoverflow.com/questions/4650209/where-to-save-application-data-in-net-application) – Andrew Morton Dec 21 '19 at 20:04
  • No, my question was not answered. I was hoping for an answer like: do the following, WebBrowser1.Navigate("file:///vbnetappfilename/bin/debug/htmlfile.html") or don't put it in debug because. – Ed Jenkins Dec 21 '19 at 21:06
  • So, perhaps an answer like `WebBrowser1.Navigate(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "MyAppName", "htmlfile.html"))`? – Andrew Morton Dec 21 '19 at 21:08
  • I don't seem to be able to communicate my question...where should the html file reside, the one that creates a table etc. I want it to be within the vb.net app files, e.g., in the bin folder or in the debug folder or in the vb.net app folder. – Ed Jenkins Dec 21 '19 at 21:47
  • If you put (perhaps a copy of) the HTML file in a subdirectory of the directory given by `Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData` then the program will have write access to it. A normal user does not have write access to the Program Files directory (it's an anti-virus thing). – Andrew Morton Dec 21 '19 at 21:55
  • I can put the html file anywhere I want, I don't need getfolderpath at least I don't think I need it. I don't know what specialfolder is. Right now the html file is located in :"file:///C:/users/jenki/desktop/KlexyHTML_2019-12-17_EJ.html" and works fine. I would like to move it inside the program so that it is part of the program. – Ed Jenkins Dec 21 '19 at 22:36
  • so then just put it in your project folder, and and change the "copy to output folder" option to "copy always" (you do this in the properties of the file when you select the file in Visual Studio's Solution Explorer). That way it will always be copied to the appropriate \bin folder when you build the application. – ADyson Dec 21 '19 at 22:43
  • Does that mean I don't use WebBrowser.Navigate? If I am not being to pushy could you walk me through the steps you just said. – Ed Jenkins Dec 21 '19 at 22:58
  • The webbrowser control still needs to be told what file to load. But you wouldn't need a whole path to it, just the filename, because the file would be in the same folder as the executing code. As for a walkthrough, not sure what you mean...my comment _is_ the walkthrough. You can probably find generic Visual Studio docs for those specific individual steps, if you need them. – ADyson Dec 21 '19 at 23:38
  • That was the first thing I tried but it cant find the file. I put it right next to the app .exe file and then used this code: WebBrowser1.Navigate("file:///KlexyHTML_2019-12-17_EJ.html") – Ed Jenkins Dec 21 '19 at 23:48
  • 1
    Use `Application.StartupPath` to get the program folder path. you can then use `Path.Combine` to create the file path. – jmcilhinney Dec 22 '19 at 00:22
  • Ah. If you're putting it into a `file:///` URL then yes you probably need an absolute path. Follow the instructions in the comment above to create that. – ADyson Dec 22 '19 at 00:35
  • Problem solved. I thought that if the html file was added to the same folder as the .exe file it wouldn't be necessary to use the entire startup path in the WebBrowser1.Navigate filename. It requires everything from c on. I tried installing it and apparently it will do what it has to do in other machines. – Ed Jenkins Dec 22 '19 at 02:34

2 Answers2

1

Application.StartupPath will get the program folder path. Then use Path.Combine to create the file path.

Simcha
  • 99
  • 1
  • 12
0

All of the many comments were very helpful in understanding how this task should be solved, however there were several important omissions. The following instructions answer the original question. I use Visual Studio 2019 Community.

First add the html file to the project by clicking on Project and then, click on Add an existing item. Find the html file and click OK. the file name will appear in the solution explorer under Form1.vb items. As many of the comments suggested, you need to use Application.StartupPath to get the beginning of the final path needed to start the program and load the html display into the VB.NET app. The reason you need the startup path is because the path will be different from one machine to another and the version of windows (7, 8.1 or 10) used. Since in my case the html5/css3/javascript filename was known (I wrote it) there was no need to use Path.Combine as suggested by several in the comments above. Loading a file into a vb.net app requires the following syntax: "file:///Application.StartupPath.....html". By adding the following code into my Form Load Sub, everything works as expected...in Windows 7, 8.1 and 10.

WebBrowser1.Navigate("file:///" + Application.StartupPath + "\KlexyHTML_2019-12-17_EJ.html")
Ed Jenkins
  • 133
  • 10