0

I have a function that writes a html file and opens it to a web browser. However when I package and deploy the application it will not open and says access denied. I am told I need to write the html file to the computers mydocuments and open it there. Any ideas on how to do this so I can get around this permission error? Here is my function that writes the html file:

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);
    }
}
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
tech127956
  • 23
  • 6
  • Well yeah that gets me the path to mydocuments but what do i put to have the file save to there? Right now the way I understand my code it just opens in the default web browser. – tech127956 Oct 04 '21 at 19:41
  • Do I just put the path to mydocuments before the report before the using statement. – tech127956 Oct 04 '21 at 19:42
  • This is where doing a bit of research pays dividends. [What is the best way to combine a path and a filename in C#/.NET?](https://stackoverflow.com/q/1048129/215552) – Heretic Monkey Oct 04 '21 at 19:43

1 Answers1

0
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);
    }
}
Victor Procure
  • 886
  • 1
  • 6
  • 17