| Writing files with the TextWriter in ASP.NET |
|
|
|
| Work - ASP.NET | |||
| Written by mbrock | |||
| Thursday, 05 June 2008 22:08 | |||
|
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.
|





