Here is your solution:
Just replace your try catch block with my code and it will work:
Here is my code
try
{
using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
{
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
using (StreamWriter sw = new StreamWriter("TestFile.txt"))
{
sw.Write("\n");
sw.WriteLine("line\n");
sw.WriteLine("test");
}
}
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
Console.WriteLine("File: " + e.Name);
Console.WriteLine("Folder:" + Path.GetDirectoryName(e.FullPath));
Console.WriteLine(d.Message);
continue;
}
And I have few suggestions for you:
1. While writing code every time you open a bracket immediately place it's corresponding closing bracket and then go back inside the bracket and then type. This rule applies not only to brackets but for all the codes which has corresponding opening and closing code. For example doublequotes(") and single quotes(') and all of them.
2. When you post your code on Daniweb always enclose the code in code tags like this "
string str = "this is a string";
"
This will display as
string str = "this is a string";
Well goodluck
If you have any problem send messages directly at
info@bizpersonnel.com
www.bizpersonnel.com
A Software development Company