我在写文件的时候用Line 26: {
Line 27: StreamWriter sw = File.AppendText("Text.txt");
Line 28:   sw.WriteLine("hello");
Line 29:   sw.Flush();
Line 30:   sw.Close();提示sw.WriteLine("hello"); CS1519: Invalid token '(' in class, struct, or interface member declaration请问哪里错了

解决方案 »

  1.   

    sw.WriteLine("hello");
    sw.Flush();
    sw.Close();
    放到方法快中
      

  2.   

    using System;
    using System.IO;class Test 
    {
        public static void Main() 
        {
            string path = @"c:\temp\MyTest.txt";
            // This text is added only once to the file.
            if (!File.Exists(path)) 
            {
                // Create a file to write to.
                using (StreamWriter sw = File.CreateText(path)) 
                {
                    sw.WriteLine("Hello");
                    sw.WriteLine("And");
                    sw.WriteLine("Welcome");
                }
            }        // This text is always added, making the file longer over time
            // if it is not deleted.
            using (StreamWriter sw = File.AppendText(path)) 
            {
                sw.WriteLine("This");
                sw.WriteLine("is Extra");
                sw.WriteLine("Text");
            }         // Open the file to read from.
            using (StreamReader sr = File.OpenText(path)) 
            {
                string s = "";
                while ((s = sr.ReadLine()) != null) 
                {
                    Console.WriteLine(s);
                }
            }
        }
    }老虎传送门
      

  3.   

    Invalid token '(' in class, struct, or interface member declaration)把值放到类结构或者接口中···
      

  4.   


     using(StreamWriter sw = File.AppendText("Text.txt"))
    {
        sw.WriteLine("hello");
    }MSDNhttp://msdn.microsoft.com/zh-cn/library/system.io.file.appendtext.aspx