rt
通过代码在程序所在的路径下建立一个新建的txt文档,并在此文档里写入预定的内容。再加深一次,如何建立一个自定义扩展名的文档,比如a.abc,并在里写入预定的内容?

解决方案 »

  1.   

    FileStream stream = new FileStream("a.abc", FileMode.Create);
                StreamWriter writer = new StreamWriter(stream);
                writer.WriteLine("hello world");
                writer.Close();
                stream.Close();
      

  2.   

    创建文件,文件名可以随意。        //path:文件路径;content:要写入的内容
            public static void WriteFile(string path, string content)
            {
                try
                {
                    using (StreamWriter sw = new StreamWriter(path, false, Encoding.UTF8))
                    {
                        sw.Write(content);
                    }
                }
                catch(Exception ex)
                {
                    throw ex;
                }
            }
      

  3.   


     using (FileStream fs = new FileStream("D:\\abc.txt", FileMode.Create, FileAccess.Write))
                {
                    StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);
                    sw.Write("你要写的内容。");
                    sw.Close();
                }
      

  4.   

    路过,我赞成1楼的先建一个FileStream再用StreamWriter写入.因为我看到MSDN是这样写的哈哈,不过我很喜欢用File类里面的方法来写入,楼主也试试吧比如:System.IO.File.WriteAllText(string path,string ContentsResizedEventArgs,Encoding.UTF8);