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

解决方案 »

  1.   

    StreamWriter srf = File.CreateText(Server.MapPath("abc.inc"));
      srf.Close();

      using (StreamWriter swf = new StreamWriter(Server.MapPath("abc.inc")))   {
     
      swf.Write("sdfdsfdsfdsf");
      swf.Close();

      }
      

  2.   

    我是这样的。请问为什么不行??if(!System.IO.File.Exists(Register.IniControl.sPath))
    {
    //实例化IniControl,在IniControl的构造函数中创建settings.ini文件。
    Register.IniControl newIniControl = new Register.IniControl(); //写入第一次运行时间,WriteFirstDate为静态方法
    IniControl.WriteFirstDate();
    //写入Notepad创建时间,WriteNotepadCreatTime为静态方法
    IniControl.WriteNotepadCreatTime();
    }如果在创建好的文件中就可以执行。在一起就不行。。奇怪。。