0

I wish to replace a TextBox, with a webbrowser to display messages in chat app

So I can add hyperlinks to videos, sites and pics, emoticons ...

But When I run the app, it cannot find the myfile.html which is in Debug folder ...

It does exists and loadable in firefox

        List<string> lines = new List<string>();
        lines.Add("<html>");
        lines.Add("<body><table width=100%><br><br>Chat:");

    lines.Add("<tr><font size=6><a href=music\\" + artist + ">
        <img src=music\\" + artist + "\\info\\default.jpg alt=" + art + " height=200 width=200 />
        </a><a href=" + path +">"+"  " + path2 +"</a></font><br><br></tr>");


        lines.Add("</table></body>");
        lines.Add("</html>");

        File.WriteAllLines("myfile.html", lines);

        wb_Messages.Navigate("myfile.html");

I have tried

 wb_Messages.Navigate("file:///myfile.html");

 wb_Messages.Navigate(Application.StartupPath + @"myfile.html");

No Joy ...

Any Help Thanks ...

dwk
  • 69
  • 1
  • 9
  • What exceptions do you get? – sommmen Apr 07 '20 at 10:37
  • Hi Web Broswer cannot find .../CHAT/Debug Make sure the path is correct ... I have used this before in c++ MFC and it worked – dwk Apr 07 '20 at 10:43
  • I have change the location to D://myfile.html ... and it cannot find the path – dwk Apr 07 '20 at 11:01
  • 1
    Just a tip: use ``Path.Combine(Application.StartupPath, "myfile.html")`` – D.Kastier Apr 07 '20 at 11:04
  • Thanks ... no Error now, but **myfile.html** does not **appear in webbrowser** ... I store a **saved webpage** , and it did not load up in **webbroswer**either . – dwk Apr 07 '20 at 11:18

2 Answers2

2

i can't use comments for this but could you try to use the following helpers i dug up from my project(s):

    /// <summary>
    /// Helpers for working with <see cref="System.IO.Path"/> or working with paths in general.
    /// </summary>
    public static class PathHelpers
    {
        /// <summary>
        /// The default output directory, based on the on the currently executing <see cref="System.Reflection.Assembly"/>
        /// </summary>
        public static string CurrentOutPutDirectory => Path.GetDirectoryName(AssemblyHelper.ExecutableAssembly.GetName().CodeBase)?.Substring(6);

        public static string CurrentOutPutDirectoryRoot => Path.GetPathRoot(CurrentOutPutDirectory);

        /// <summary>
        /// Checks if a string could be used in a path
        /// <see href="https://stackoverflow.com/questions/2435894/net-how-do-i-check-for-illegal-characters-in-a-path#comment30899588_2435894"/>
        /// </summary>
        /// <param name="path">Path to check</param>
        /// <returns></returns>
        public static bool FilePathHasInvalidChars(string path)
        {
            return (!string.IsNullOrEmpty(path) && path.IndexOfAny(System.IO.Path.GetInvalidPathChars()) >= 0 && path.IndexOfAny(System.IO.Path.GetInvalidFileNameChars()) > 0);
        }
    }

    /// <summary>
    /// Helpers for dealing with <see cref="System.Reflection.Assembly"/>
    /// </summary>
    public static class AssemblyHelper
    {

        /// <summary>
        /// The assembly that holds the executable. For this, <see cref="Assembly.GetEntryAssembly()"/> is used.
        /// This looks at the current <see cref="AppDomain"/>.
        /// <warn>A fallback value is used to get the executing assembly!</warn>
        /// </summary>
        public static Assembly ExecutableAssembly => Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly();
    }


Then use var alltext = File.ReadAllText(Path.Combine(PathHelpers.CurrentOutPutDirectory, "YourFileIntheDebugFolder.txt)) (pseudo code) to check if it works and loads the html directly.

EDIT to display a html file in the webbrowser, use this top answer, and pass the alltext string.

https://stackoverflow.com/a/20351048/4122889

sommmen
  • 6,570
  • 2
  • 30
  • 51
1

I think it's because you use only file name instead of full path.

File.WriteAllLines("myfile.html", lines);

In this method you didn't specify full path, so it will try to save file in some default location. What location? No need to think about it now.

wb_Messages.Navigate("myfile.html");

This method tries to read data from file, but only file name is specified. It doesn't know where to find this file, so it will look in some default location also.

But default locations for both methods can be different. I don't know how it works internally, but maybe you will get some info in Microsoft documentation.

Simple solution:

// now filePath becomes "C:\something...\myfile.html"

string filePath = Path.GetFullPath("myfile.html"); 

// save data using full path

File.WriteAllLines(filePath, lines);

// navigate to file using full path

wb_Messages.Navigate(filePath);
apocalypse
  • 5,764
  • 9
  • 47
  • 95