Solved

How To Saving a File To Disk C#

Posted on 2014-02-14
2
892 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
While working on Silverlight and WCF application, I faced one issue where fault exception occurred at WCF operation contract is not getting propagated to Silverlight client. So after searching net I came to know that it was behavior by default for s…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

708 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

15 Experts available now in Live!

Get 1:1 Help Now