【问题标题】:c# load a create html file webbrowserc#加载一个创建html文件的webbrowser
【发布时间】:2020-07-19 12:40:32
【问题描述】:

我希望用浏览器替换文本框,以便在聊天应用中显示消息

这样我就可以为视频、网站和图片、表情符号添加超链接...

但是当我运行该应用程序时,它无法找到 Debug 文件夹中的 myfile.html ...

它确实存在并且可以在 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");

我试过了

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

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

没有喜悦...

任何帮助谢谢...

【问题讨论】:

  • 你有什么例外?
  • 您好 Web Broswer 找不到 .../CHAT/Debug 确保路径正确...我以前在 c++ MFC 中使用过它并且它有效
  • 我已将位置更改为 D://myfile.html ...但找不到路径
  • 提示:使用Path.Combine(Application.StartupPath, "myfile.html")
  • 谢谢 ... 现在没有错误,但是 myfile.html 没有出现在 webbrowser 中 ...我存储了一个 保存的网页 ,它也没有在 webbroswer 中加载。

标签: c# html


【解决方案1】:

我不能为此使用 cmets,但你可以尝试使用我从我的项目中挖掘出来的以下助手:

    /// <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();
    }


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

EDIT 在网络浏览器中显示一个 html 文件,使用这个最佳答案,并传递 alltext 字符串。

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

【讨论】:

    【解决方案2】:

    我认为这是因为您只使用文件名而不是完整路径。

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

    在这个方法中你没有指定完整路径,所以它会尝试将文件保存在某个默认位置。什么位置?现在不用考虑了。

    wb_Messages.Navigate("myfile.html");
    

    此方法尝试从文件中读取数据,但只指定了文件名。它不知道在哪里可以找到这个文件,所以它也会在某个默认位置查找。

    但两种方法的默认位置可能不同。我不知道它在内部是如何工作的,但也许你会在 Microsoft 文档中获得一些信息。

    简单的解决方案:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-12
      • 1970-01-01
      • 1970-01-01
      • 2015-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多