Windows程序在E:\MyForm文件夹下运行能不能E:\NewFile文件夹下面创建一个文件(如txt)。如果NewFile文件夹不存在,就创建他。

解决方案 »

  1.   

    File.Create (String, Int32, FileOptions)  
    Creates or overwrites the specified file, specifying a buffer size and a FileOptions value that describes how to create or overwrite the file.
      

  2.   

    using System.IO;
    ………………if(!File.Exsit(_FilePath))
    {
        File.Creat(_FilePath);
    }
      

  3.   

    if(!Directory.Exits(path))
    {
     Directory.Create(path);
    }
    //do
      

  4.   

    string dir = MapPath(DirectoryName);
    try
    {
       if (!Directory.Exists(dir))
       {
          Directory.CreateDirectory(dir);
       }
    }
      

  5.   

    Exists
    hoho 更正 更正 二师兄的也没写对 面壁10分钟
      

  6.   

    string dir = "E:\\FileCreated\\test.txt";            if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }上面的程序是在E盘上生成了两个文件夹(Fi*和Test.txt),
    并没有生成文本文件啊?
      

  7.   

    if(!File.Exsit(dir))
    {
        File.Creat(dir);
    }
      

  8.   

    using System;
    using System.IO;
    using System.Text;class Test
    {
        public static void Main()
        {
            string path = @"c:\temp\MyTest.txt";        try
            {            // Delete the file if it exists.
                if (File.Exists(path))
                {
                    // Note that no lock is put on the
                    // file and the possibliity exists
                    // that another process could do
                    // something with it between
                    // the calls to Exists and Delete.
                    File.Delete(path);
                }            // Create the file.
                using (FileStream fs = File.Create(path))
                {
                    Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
                    // Add some information to the file.
                    fs.Write(info, 0, info.Length);
                }            // Open the stream and read it back.
                using (StreamReader sr = File.OpenText(path))
                {
                    string s = "";
                    while ((s = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(s);
                    }
                }
            }        catch (Exception Ex)
            {
                Console.WriteLine(Ex.ToString());
            }
        }
    }
      

  9.   

    还是MSDN好,
    CSDN使人变懒不过能让人少走弯路
      

  10.   

    2,3,4楼的exist三种拼法,哈哈....