Link to home
Start Free TrialLog in
Avatar of jmpatton
jmpatton

asked on

How To Saving a File To Disk C#

Hello,

I need to save a html file that I have created on the fly from within my WCF service.  How can I do this?

Here is the code i'm using to create the file

using (FileStream fs = new FileStream("fileName.html", FileMode.Create))
            {
                using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
                {
                    w.WriteLine("<html>");
                    w.WriteLine("<head><title>File Title</title></head>");
                    w.WriteLine("<body>");
                    //stuff here
                    w.WriteLine("</body>");
                    w.WriteLine("</html>");

                }

            }

//ToDo: Save HTML file to c: drive

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Try below cod snippet.


StringBuilder str = new StringBuilder();
str.Append(@"<html>").
str.Append(@"..........");  // Fill the text as you wish 

System.IO.File.WriteAllText(@"C:\Users\Public\TestFolder\WriteText.html", str.ToString());

Open in new window