Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How To Saving a File To Disk C#

Posted on 2014-02-14
2
Medium Priority
?
986 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

670 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