我尝试过如下。
private void btnPrint_Click(object sender, RoutedEventArgs e)
{
Newwebbrowser.Navigate("test.html");
IOleServiceProvider sp = Newwebbrowser.Document as IOleServiceProvider;
if (sp != null)
{
Guid IID_IWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046");
Guid IID_IWebBrowser2 = new Guid("D30C1661-CDAF-11d0-8A3E-00C04FC9E26E");
const int OLECMDID_PRINT = 6;
const int OLECMDEXECOPT_DONTPROMPTUSER = 2;
dynamic wb; // will be of IWebBrowser2 type, but dynamic is cool
sp.QueryService(IID_IWebBrowserApp, IID_IWebBrowser2, out wb);
if (wb != null)
{
// note: this will send to the default printer, if any
wb.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, null, null);
}
}
}
[ComImport, Guid("6D5140C1-7436-11CE-8034-00AA006009FA"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IOleServiceProvider
{
[PreserveSig]
int QueryService([MarshalAs(UnmanagedType.LPStruct)] Guid guidService, [MarshalAs(UnmanagedType.LPStruct)] Guid riid, [MarshalAs(UnmanagedType.IDispatch)] out object ppvObject);
}
Html文件已在驱动器中。 我收到了以下错误
System.dll
中发生了'System.UriFormatException'类型的未处理异常其他信息:无效的URI:无法确定URI的格式。
Newwebbrowser.Navigate(“test.html”); //这里我得到了异常
答案 0 :(得分:1)
错误消息表示它所说的内容 - 它不知道“test.html”的含义(或它在哪里)。
使用“file:/// c:\ test \ test.html”或“http://localhost/test.html”任何合适的内容,例如:
“file:///”+ AppDomain.CurrentDomain.BaseDirectory +“test.html”