WPF外部打开html文件
注意:这是一份提供WPF外部浏览器打开html的方法,而不是WPF内部嵌入html
需要通过浏览器打开,否则无法使用地址栏拼接参数的形式操作html
下面是打开html的方法↓
string localHtmlPath = @"C:\Users\pangb\Downloads\Help\帮助文档 - 副本.html";if (File.Exists(localHtmlPath)){try{string browserPath = GetDefaultBrowserPath();string fullUri = new Uri(localHtmlPath).AbsoluteUri + "?page=first.html";Process.Start(browserPath, $"\"{fullUri}\"");}catch (Exception ex){MessageBox.Show($"打开文件时出错: {ex.Message}");}}else{MessageBox.Show("HTML文件未找到!");}
这是获取浏览器的方法↓
private string GetDefaultBrowserPath(){try{string name = Microsoft.Win32.Registry.GetValue(@"HKEY_CLASSES_ROOT\http\shell\open\command", "", null) as string;if (name != null){// 清理路径(移除参数和引号)if (name.Contains("\""))name = name.Substring(1, name.IndexOf('"', 1) - 1);return name;}}catch{// 忽略错误,返回空字符串}return null;}