Solved

How To Saving a File To Disk C#

Posted on 2014-02-14
2
897 Views
Last Modified: 2014-03-12
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

0
Comment
Question by:jmpatton
2 Comments
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39860859
Hi jmpatton;

Try the modified code below.
You state, "//ToDo: Save HTML file to c: drive", Change this string "fileName.html" to include the file path as well to where you want it stored on the C: drive.

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>");

        // Write all unwritten buffers out to the file
        // The using statement should end up closing the file
        w.Flush();
    }

}

Open in new window

0
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 39861689
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

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now