//Read a Text File///////////////////////////////////////////////////////////////////////////////////////////////////
using
System;
using
System.IO;
namespace
readwriteapp
{
class
Class1
{
[STAThread]
static
void
Main(
string
[] args)
{
String line;
try
{
//Pass the file path and file name to the StreamReader constructor
StreamReader sr =
new
StreamReader(
"C:\\Sample.txt"
);
//Read the first line of text
line = sr.ReadLine();
//Continue to read until you reach end of file
while
(line !=
null
)
{
//write the lie to console window
Console.WriteLine(line);
//Read the next line
line = sr.ReadLine();
}
//close the file
sr.Close();
Console.ReadLine();
}
catch
(Exception e)
{
Console.WriteLine(
"Exception: "
+ e.Message);
}
finally
{
Console.WriteLine(
"Executing finally block."
);
}
}
}
}
//Write a text file////////////////////////////////////////////////////////////////////////////////////////////////////
using
System;
using
System.IO;
using
System.Text;
namespace
readwriteapp
{
class
Class1
{
[STAThread]
static
void
Main(
string
[] args)
{
Int64 x;
try
{
//Open the File
StreamWriter sw =
new
StreamWriter(
"C:\\Test.txt"
,
true
, Encoding.ASCII);
//Writeout all the lines changing the coordinates.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//
//
// HOW CAN I DO THIS???
//
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//close the file
sw.Close();
}
catch
(Exception e)
{
Console.WriteLine(
"Exception: "
+ e.Message);
}
finally
{
Console.WriteLine(
"Executing finally block."
);
}
}
}
}