c# - 如何在 C# winforms 应用程序中将 html 文件保存到 MyDocuments
问题描述
我有一个编写 html 文件并将其打开到网络浏览器的函数。但是,当我打包和部署应用程序时,它不会打开并说访问被拒绝。我被告知我需要将 html 文件写入计算机 mydocuments 并在那里打开它。有关如何执行此操作的任何想法,以便我可以解决此权限错误?这是我编写 html 文件的函数:
private void PrintReport(StringBuilder html)
{
// Write (and overwrite) to the hard drive using the same filename of "Report.html"
try
{
// A "using" statement will automatically close a file after opening it.
// It never hurts to include a file.Close() once you are done with a file.
using (StreamWriter writer = new StreamWriter("Report.html"))
{
writer.WriteLine(html);
}
System.Diagnostics.Process.Start(@"Report.html"); //Open the report in the default web browser
}
catch (Exception ex)
{
MessageBox.Show(message + ex.Message, "Program Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
解决方案
private void PrintReport(StringBuilder html)
{
// Write (and overwrite) to the hard drive using the same filename of "Report.html"
try
{
// A "using" statement will automatically close a file after opening it.
// It never hurts to include a file.Close() once you are done with a file.
using (StreamWriter writer = new StreamWriter(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Report.html"))
{
writer.WriteLine(html);
}
System.Diagnostics.Process.Start(@"Report.html"); //Open the report in the default web browser
}
catch (Exception ex)
{
MessageBox.Show(message + ex.Message, "Program Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
推荐阅读
- powershell - Runbook Powershell Invoke-Sqlcmd SQL Server MI 连接
- ios - 在uitableview中过滤后执行segue
- r - 如何一次读取/导入 CSV 并创建代码,这样您就不必再次读取/导入
- c++ - C++,删除函数/迭代过程中定义的动态数组
- ruby-on-rails - 放置路由命令后需要填写 URI 路径、控制器和操作/方法的问题,但我不确定是否要查找
- javascript - 为什么这个 webrtc 流媒体只能在本地连接上工作?
- opencv - 理解霍夫变换
- javascript - 如何将特定值从一页转移到另一页?
- python - 无法识别术语“Select-String”
- java - 从另一个对象列表创建对象的元素列表