Reputation: 89
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 ...
Upvotes: 0
Views: 511
Reputation: 5884
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);
Upvotes: 1
Reputation: 7658
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
Upvotes: 2