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.

 

 
Comments (2)
how to add ajax toolkit to already under construction website
2 Thursday, 18 August 2011 08:32
harmeet
can u please .. i am in urgent need of it.... ur article was really of grt help ... but i really didnt get benefitted... i have already build a website but the website is as yet not hosted globally for netusers..... will be soon hosting it.... but i really wanted to add AJAX toolkit features into my website...HOW CAN I DO SO NOW....
was following the step given but the same error appeared even after installing ajexext.exe(ver 2) for 2008 vs....
not wot am i supposed to do further...
Helpp me in urgent basis please....
thank u in anticipation :)
Thnx.!
1 Friday, 21 January 2011 03:47
fidan
Thanks.!

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
Copyright © 2012 mbrock.com. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.