DESKTOP

Create, Read, and Write a Text File

9/25/2010 3:11:15 PM
If you tell .NET that you are opening a text file, the underlying stream is wrapped in a text decoder called StreamReader or StreamWriter that can translate between UTF-8 text and the raw bytes.

Here is a sample program that writes its command-line arguments to a text file that you specify and then reads it back:

class Program
{
static void Main(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine(
"Usage: ConsoleToFile filename output1 output2 output3 ...");
return;
}
//write each command line argument to the file
string destFilename = args[0];
using (StreamWriter writer = File.CreateText(destFilename))
{
for (int i = 1; i < args.Length; i++)
{
writer.WriteLine(args[i]);
}
}
Console.WriteLine("Wrote args to file {0}", destFilename);

//just read back the file and dump it to the console
using (StreamReader reader = File.OpenText(destFilename))
{
string line = null;
do
{
line = reader.ReadLine();
Console.WriteLine(line);
} while (line != null);
}
}
}


Note

For this and all succeeding code examples in this chapter, make sure you have using System.IO; at the top of your file, as it will not always be explicit in every code sample.


Note

Files are shared resources, and you should take care to close them when done to return the resource back to the operating system. This is done using the Dispose Pattern,  “Memory Management.” The using statements in the preceding code sample ensure that the files are closed at the end of each code block, regardless of any exceptions that occur within the block. You should consider this a best practice that you should always follow whenever possible.

Other  
  •  Opening a Local File from a Silverlight Application
  •  Navigating Architecture Changes in Windows Vista
  •  Getting Around Windows Vista
  •  Managing User Account Control and Elevation Prompts
  •  Supporting Computers Running Windows Vista
  •  Using System Support Tools in Vista
  •  Managing System Properties
  •  Introducing Automated Help And Support in Vista
  •  Working with the Automated Help System
  •  Installing and Maintaining Devices in Vista: The Essentials
  •  Getting Started with Device Manager
  •  Working with Device in Vista
  •  Managing Hardware in Vista
  •  Customizing Hardware Device Settings
  •  Managing Internet Time in Vista
  •  Optimizing Windows Vista Menus
  •  Customizing the Taskbar in Vista
  •  Optimizing Toolbars in Vista
  •  Working with Desktop Themes in Vista
  •  Optimizing the Desktop Environment in Vista
  •  
    Video
    Top 10
    HP Spectre XT Touchsmart Review - Everything Is Fine Except Battery Life (Part 2)
    HP Spectre XT Touchsmart Review - Everything Is Fine Except Battery Life (Part 1)
    Lenovo Thinkpad Carbon Touch Ultrabook Review (Part 3)
    Lenovo Thinkpad Carbon Touch Ultrabook Review (Part 2)
    Lenovo Thinkpad Carbon Touch Ultrabook Review (Part 1)
    Blackberry 10 OS (Part 3)
    Blackberry 10 OS (Part 2)
    Blackberry 10 OS (Part 1)
    How To Keep High-End Speakers In Good Conditions
    How To Use TV As An Audio Converter
    Most View
    Windows Small Business Server 2011 : Managing User Roles
    Canon IXUS 510 HS
    Review: Nikon D4 – The master of the dark arts (Part 2)
    Sharepoint 2007: Use the My Links to Manage Your Links
    Every Cloud...(Part 2) - Mobility & Portability
    Searching for Google’s future (Part 1) - Taking the tablets
    Dell Inspiron 17R Special Edition - Battery Is A Disappointment
    10 Valuable TVs For Your Living Room
    Silverlight Recipes : Creating Silverlight Using Ruby, Python, or JScript
    Build A Home Theatre PC (Part 2)
    The Library In Your Pocket (Part 2) - iOS – iPad, Android – Kobo Vox, Kindle Fire, Binatone ReadMe, Google Nexus 7
    Samsung 830
    Windows 9 : What to expect - 32-bit support , WinRT & XNA
    Programming with DirectX : View Transformations
    Securing Your Silverlight Application
    Programming the Mobile Web : Mobilizing WordPress and Other CMSs
    Aerocool Silent Master Blue
    Getting Started with Device Manager
    Calendar Plus - Simple And Friendly
    Transact-SQL in SQL Server 2008 : New date and time Data Types and Functions