0

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

    }
4

3 回答 3

5

您可以在语句之前声明它if,但不进行赋值 - 然后确保在两个分支中都为其赋值:

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

File.AppendText但是,如果文件存在,您仍然打开文件这一事实很可能会给您带来问题 -如果您发现它仍然是空的,则不清楚您为什么要打电话。您实际上只是想创建或附加吗?如果是这样,只需使用AppendText- 就可以了。

您还应该使用using语句自动关闭编写器......如果您确实需要@"C:\data\" + path在多个地方使用,我会将该通用表达式提取到局部变量中:

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

如果您坚持使用File.AppendTextStreamWriter构造函数,请考虑使用条件表达式:

TextWriter outFile = inFile.Peek() == -1 || inFile == null
    ? File.AppendText(file) : new StreamWriter(file);
于 2013-11-09T08:31:28.280 回答
3

这是设计使然。您需要编写如下内容:

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

在这种情况下,对 outfile的分配null是可选的,但是当您开始使用 C++ 进行编码时,您会发现这是一个很好的做法。

于 2013-11-09T08:32:08.653 回答
1

请在外部声明它并在内部实例化它,这是正常行为,如果您在代码块内声明一个仅在该代码块内可用的变量,那么请在 CodeBlock 外部声明该变量并在您的内部实例化或分配一些东西if else代码块

错误的

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

正确的

string s ; 
if ( condition ) 
{ 
  s = "jo";
} 
MessageBox.Show(s) ;
于 2013-11-09T08:36:12.377 回答