static void Main(string[] args)
        {
            try
            {
                FileStream fs = new FileStream(@"4.txt", FileMode.OpenOrCreate);
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine("hello!");
                sw.Close();
                fs.Close();
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadKey();
        }
代码如上,总是不能创建文件。各位帮忙查找原因。

解决方案 »

  1.   

    先引用一下System.IO这个命名空间
    然后使用其中的stream操作类
    streamwriter strwriterobj =new streamwriter ();
    strwriterobj= file.createtext("(这个地方写桌面的地址)aspnet.txt" );
    strwriterobj.writeline( "hello world!!!" ) "//向文件中写入内容
    strwriterobj.close ()//完成操作,关闭流对象 
      

  2.   

    FileStream fs = new FileStream(@"d:\4.txt", FileMode.OpenOrCreate);
    d:\4.txt 把路径写死看看!
      

  3.   

    再发一遍代码:using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;namespace 综合练习1
    {
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    FileStream fs = new FileStream(@"4.txt", FileMode.OpenOrCreate);
                    StreamWriter sw = new StreamWriter(fs);
                    sw.WriteLine("hello!");
                    sw.Close();
                    fs.Close();
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                Console.ReadKey();
            }
        }
    }
      

  4.   

     FileStream fs = new FileStream(Server.MapPath("4.txt"), FileMode.OpenOrCreate);
      

  5.   

     string path = Directory.GetCurrentDirectory();
    看看路径