简体   繁体   English

c# 加载创建 html 文件 webbrowser

[英]c# load a create html file webbrowser

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 ...但是当我运行该应用程序时,它找不到位于Debug 文件夹中的myfile.html ...

It does exists and loadable in firefox它在 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...任何帮助谢谢...

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.然后使用var alltext = File.ReadAllText(Path.Combine(PathHelpers.CurrentOutPutDirectory, "YourFileIntheDebugFolder.txt)) (伪代码)检查它是否有效并直接加载 html。

EDIT to display a html file in the webbrowser, use this top answer, and pass the alltext string.编辑以在网络浏览器中显示 html 文件,使用此最佳答案,并传递alltext字符串。

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

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.我不知道它在内部是如何工作的,但也许你会在 Microsoft 文档中获得一些信息。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM