Solved

How To Saving a File To Disk C#

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

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

636 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