Home Work ASP.NET Writing files with the TextWriter in ASP.NET
Writing files with the TextWriter in ASP.NET PDF Print E-mail
Work - ASP.NET
Written by mbrock   
Thursday, 05 June 2008 22:08

ASP.netEvery once in a while you may find yourself needing to write a file to the web server. For instance, I recently had to write an ASP.NET page that would save certain data from an input XML file as a text file on the web server. Thanks to the nifty abilities of C#, saving the new file was a snap. Read on for more...

Below is a simple C# function to write a file. I have used this function in both Windows console applications and in ASP.NET applications. It is a very simple function - or "method" depending on your background.

using System.IO;
 
private void CreateOutPutFile(string strFileContents, string strOutFileName)
{
    TextWriter tw = new StreamWriter(strOutFileName);
 
    try
    {
        tw.WriteLine(strFileContents);
        tw.Close();
    }
    catch (System.Exception e)
    {
    //display an error message here...
    }
 
}

 

This function takes two string input parameters. The first parameter should be a string that contains the contents of the file. And the second parameter is the full file name including the path to the file.

The function simply uses a TextWriter to create the output file and to put the contents into the file. You must reference System.IO for the TextWriter.

 

 

Add your comment

Your name:
Your email:
Your website:
Subject:
Comment:
  The word for verification. Lowercase letters only with no spaces.
Word verification:
mbrock.com
This site, it's contents, and "The Original Web Empire"
Copyright © 1999 - 2010 mbrock.com. All Rights Reserved.