从 .txt 加载字符串并在 Visual Studio 的 Web 浏览器中显示 HTML-file
Load string from .txt and display HTML-file in a webBrowser in VisualStudio
我在 Visual Studio 2017 年工作,我得到了一个包含 HTML 文件的文件夹,我想在 webBrowser-Element 中显示这些文件。
我尝试了不同的方法,但总是得到 NULL 异常,并且想找到一种方法来从 .txt 文件加载字符串(包含 HTML 的文件路径)并导航我的 webBrowser-此文件路径的元素。
try
{
//Pass the file path and file name to the StreamReader constructor
StreamReader sr = new StreamReader("C:/install/Win10-Tipps/link.txt");
line = sr.ReadLine();
//Continue to read until you reach end of file
while (line != null)
{
//write the lie to console window
Console.WriteLine(line);
//Read the next line
line = sr.ReadLine();
Console.WriteLine(line);
Uri link = new Uri(line);
Console.WriteLine(link);
webBrowser1.Navigate(link);
}
//close the file
sr.Close();
}
catch (Exception e)
{
Console.WriteLine("Exception");
}
它打印 "file://C:/Install/Win10-Tipps/TippsHTML/TippSchnellePcSperre.html" 这是正确的文件路径,但也打印“"System.ArgumentNullException" in System.dll”
假设我理解你的意图:
我得到了一个名为 "pathToWebsite.txt" 的文本文件,其中包含 html 文件的路径(在我的例子中是 C:\test.html).
阅读 html 的路径非常简单:
string url = null;
using(StreamReader reader = new StreamReader("C:\pathToWebsite.txt"))
{
//If the textfiles only contains one url in the first line.
url = reader.ReadLine();
}
然后我可以简单地告诉我的 webBrowser 导航到该路径:
if(url != null)
{
//Maybe a check to make sure url is a valid path to a html page.
webBrowser1.Navigate(url);
}
我在 Visual Studio 2017 年工作,我得到了一个包含 HTML 文件的文件夹,我想在 webBrowser-Element 中显示这些文件。
我尝试了不同的方法,但总是得到 NULL 异常,并且想找到一种方法来从 .txt 文件加载字符串(包含 HTML 的文件路径)并导航我的 webBrowser-此文件路径的元素。
try
{
//Pass the file path and file name to the StreamReader constructor
StreamReader sr = new StreamReader("C:/install/Win10-Tipps/link.txt");
line = sr.ReadLine();
//Continue to read until you reach end of file
while (line != null)
{
//write the lie to console window
Console.WriteLine(line);
//Read the next line
line = sr.ReadLine();
Console.WriteLine(line);
Uri link = new Uri(line);
Console.WriteLine(link);
webBrowser1.Navigate(link);
}
//close the file
sr.Close();
}
catch (Exception e)
{
Console.WriteLine("Exception");
}
它打印 "file://C:/Install/Win10-Tipps/TippsHTML/TippSchnellePcSperre.html" 这是正确的文件路径,但也打印“"System.ArgumentNullException" in System.dll”
假设我理解你的意图: 我得到了一个名为 "pathToWebsite.txt" 的文本文件,其中包含 html 文件的路径(在我的例子中是 C:\test.html).
阅读 html 的路径非常简单:
string url = null;
using(StreamReader reader = new StreamReader("C:\pathToWebsite.txt"))
{
//If the textfiles only contains one url in the first line.
url = reader.ReadLine();
}
然后我可以简单地告诉我的 webBrowser 导航到该路径:
if(url != null)
{
//Maybe a check to make sure url is a valid path to a html page.
webBrowser1.Navigate(url);
}