gpt4 book ai didi

C# 将字符串写入 WebBrowser 控件中加载的 html 页面中 %name% 中定义的变量

转载 作者:行者123 更新时间:2023-11-28 01:16:05 25 4
gpt4 key购买 nike

我在 WinForm 的 WebBrowser 控件中加载了一个 html 本地文件。在html代码中,我在%variables%中定义了一些变量。

我的问题是如何将字符串/数据从 WinForm 传输/引用到加载的 html 页面中定义的 %viarables%,并刷新显示在 WebBrowser 上的加载的 html 页面。

以下是加载本地html文件的代码。任何建议将不胜感激。

顺便说一句,它是一个 WinForm 应用程序,而不是 asp.Net。

        string curDir = Directory.GetCurrentDirectory();
this.webBrowser1.Url = new Uri(String.Format("file:///{0}/{1}", curDir + "\\Forms", fileName));

最佳答案

与其在加载 HTML 后尝试替换变量(这可能会出现问题),不如读取源文件、替换变量,然后将更新后的文档加载到 Web 浏览器控件中?这看起来要简单得多。

例如:

string dir = Path.Combine(Directory.GetCurrentDirectory(), "forms");
string html = File.ReadAllText(Path.Combine(dir, fileName));
foreach (string variable in GetListOfVariables())
{
html = html.Replace(variable, GetReplacementForVariable(variable));
}
webBrowser1.DocumentText = html;

关于C# 将字符串写入 WebBrowser 控件中加载的 html 页面中 %name% 中定义的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35656417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com