سؤال

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);
                }
            }
        }

    }
هل كانت مفيدة؟

المحلول

You can declare it before the if statement, but with no assignment - and then make sure that you assign a value to it in both branches:

TextWriter outFile;
if (inFile.Peek() == -1 || inFile == null)
{
    outFile = File.AppendText(@"C:\data\" + path);
}
else
{
    outFile = new StreamWriter(@"C:\data\" + path);
}

However, the fact that you still have the file open if it exists may well cause you problems - and it's not clear why you're calling File.AppendText if you've found that it's empty anyway. Are you actually just trying to create or append? If so, just use AppendText - it'll be fine.

You should also use a using statement to close the writer automatically... and if you do need to use @"C:\data\" + path in several places, I'd extract that common expression to a local variable:

string file = Path.Combine(@"c:\data", path);
// Now use file everywhere

If you do stick to using File.AppendText or the StreamWriter constructor, consider using a conditional expression:

TextWriter outFile = inFile.Peek() == -1 || inFile == null
    ? File.AppendText(file) : new StreamWriter(file);

نصائح أخرى

That's by design. You need to write something like:

TextWriter outFile = null; 
if (inFile.Peek() == -1 || inFile == null)
{
    outFile = File.AppendText(@"C:\data\" + path);
}
else
{
    outFile = new StreamWriter(@"C:\data\" + path);
}

The assignment of null to outfile is optional in this context, but when you start coding in C++ sometime, you will find it's a good practice.

Please declare it outside and instantiate it inside , It's the normal behavior , IF you declare a variable inside a code block that will be available only withing that code block , So please declare the variable outside of CodeBlock and instantiate or assign something to it inside your if else code block

Wrong

if ( condition ) 
{
 string s = "J" ;  
}
MessageBox.Show(s) 

Correct

string s ; 
if ( condition ) 
{ 
  s = "jo";
} 
MessageBox.Show(s) ;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top