When I instantiate the Textwriter object in the if/else clause, it is not visible outside the clause. What can I do here? I want to either append to or write a new file.
Here is the code:
private static void eventfull_doing()
{
string path, line;
int countLines;
Console.Write("Enter the name(with extension) of the file in the \"data\" folder, or create a new name(with extension): ");
path = Console.ReadLine();
TextReader inFile = new StreamReader(@"C:\data\" + path);
Console.Write("How many lines of text do you want to add to the file?: ");
countLines = Convert.ToInt32(Console.ReadLine());
if (inFile.Peek() == -1 || inFile == null)
{
TextWriter outFile = File.AppendText(@"C:\data\" + path);
}
else
{
TextWriter outFile = new StreamWriter(@"C:\data\" + path);
}
for (int i = 0; i < countLines; i++)
{
Console.Write("Enter line: ");
line = Console.ReadLine();
outFile.
}
Here is what I've done: Notice that in the first snippet, the if condition is not what was intended.
string path, line;
int countLines;
Console.Write("Enter the name(with extension) of the file in the \"data\" folder, or create a new name(with extension): ");
path = Console.ReadLine();
string file = Path.Combine(@"c:\data", path);
Console.Write("How many lines of text do you want to add to the file?: ");
countLines = Convert.ToInt32(Console.ReadLine());
TextWriter outFile = null;
if (File.Exists(file))
{
using (outFile = File.AppendText(file))
{
for (int i = 0; i < countLines; i++)
{
Console.Write("Enter line: ");
line = Console.ReadLine();
outFile.WriteLine(line);
}
}
}
else
{
using (outFile = new StreamWriter(file))
{
for (int i = 0; i < countLines; i++)
{
Console.Write("Enter line: ");
line = Console.ReadLine();
outFile.WriteLine(line);
}
}
}
}