如何使用c#以编程方式将html字符串直接传递给web浏览器?
例如,我有一些简单的控制台应用程序。我的目的是生成一些报告,并在一些外部应用程序(如记事本或web浏览器)中向用户表示如何使用c#以编程方式将html字符串直接传递给web浏览器?,c#,browser,notepad,C#,Browser,Notepad,例如,我有一些简单的控制台应用程序。我的目的是生成一些报告,并在一些外部应用程序(如记事本或web浏览器)中向用户表示 class Program { [DllImport("user32.dll", EntryPoint = "FindWindowEx")] public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass
class Program
{
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);
static void Main()
{
OpenNotepadAndInputTextInIt("Message in notepad");
OpenHtmlFileInBrowser(@"c:\temp\test.html");
}
private static string GetDefaultBrowserPath()
{
string key = @"HTTP\shell\open\command";
using(RegistryKey registrykey = Registry.ClassesRoot.OpenSubKey(key, false))
{
return ((string)registrykey.GetValue(null, null)).Split('"')[1];
}
}
private static void OpenHtmlFileInBrowser(string localHtmlFilePathOnMyPc)
{
Process browser = Process.Start(new ProcessStartInfo(GetDefaultBrowserPath(), string.Format("file:///{0}", localHtmlFilePathOnMyPc)));
browser.WaitForInputIdle();
}
private static void OpenNotepadAndInputTextInIt(string textToInputInNotepad)
{
Process notepad = Process.Start(new ProcessStartInfo("notepad.exe"));
notepad.WaitForInputIdle();
if(notepad != null)
{
IntPtr child = FindWindowEx(notepad.MainWindowHandle, new IntPtr(0), "Edit", null);
SendMessage(child, 0x000C, 0, textToInputInNotepad);
}
}
}
这个解决方案很好,但正如你所看到的,我有两种方法
GetDefaultBrowserPath()
和GetDefaultBrowserPath(字符串localHtmlFilePathInMyPc)
。第一个将消息字符串直接传递到记事本窗口,但第二个需要创建一个文件,即一些html
页面,然后将此html文件作为web浏览器的参数传递。这是一种缓慢的解决方案我想生成一个html
字符串报告,并将其直接传递到web浏览器,而无需创建中间html文件。如果您想在系统的默认浏览器应用程序中打开外部浏览器窗口(而不是将其集成到应用程序中),我认为唯一的方法是通过:
data:text/html,<html><title>Hello</title><p>This%20is%20a%20test
Process.Start("file:///the/path/here")