[置顶] C# 中文件的操作

  C#中提供文件管理的命名空间是  System.IO。其中包括了常见的文件操作,删除插入,移动等  ,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;            //引用必须添加


namespace file__test
{
    class Program
    {
        static void Main(string[] args)
        {
             //设置文件的绝对路径
            string STR_path = @"d:/file.txt";
             //创建文件
            File.Create(STR_path);
            FileInfo nn = new FileInfo(STR_path);
            if (nn.Exists)

            {
                Console.WriteLine("文件已从在");
            }
            else
            {
             Console.WriteLine("文件不从在");
            }
            Console.ReadLine();
            //删除文件
            File.Delete(STR_path);
            FileInfo ii = new FileInfo(STR_path);
             if (ii.Exists)

            {
                Console.WriteLine("文件还从在");
            }
            else
            {
             Console.WriteLine("文件已删除");
            }
            Console.ReadLine();
            string str1_path = @"d:\123345.txt";
            // 已路径为参数创建文件夹
            Directory.CreateDirectory(str1_path);
           
            DirectoryInfo info = new DirectoryInfo(str1_path);
            if (info.Exists)     //判断文件夹是否存在
            {
                Console.WriteLine("已经从在");
            }
            else
            {
                 Console.WriteLine("文件不从在") ;
            }
            Console.ReadLine();


             //directoryinfo 测试!!
            string str2_path = @"d:\123345.txt";
            if (str2_path.Length==0)
            {
                Console.WriteLine("文件夹不存在");
                Console.ReadKey();
                return;

            }
            else
            {
                str2_path = str2_path;
            }
            DirectoryInfo drinfo = null;
            try
            {
                drinfo = new DirectoryInfo(str2_path);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            DeleteFills(drinfo);
            if (drinfo.Exists)
            {
                Console.WriteLine("文件还存在");
            }
            else
            {
                Console.WriteLine("文件已删除");
            }
            Console.ReadKey();
        }
        
   
//         private static void DeleteFills(string str2_path)
//         {
//             throw new NotImplementedException();
//         }
        public static void DeleteFills(DirectoryInfo drinfo)
        {
            try
            {
                FileInfo[] files = drinfo.GetFiles();      //通过实例获取文件夹中的文件
                foreach (FileInfo f in files)
                {
                    f.Delete();     //删除文件
                }
                DirectoryInfo[] dirs = drinfo.GetDirectories();   //通过实例获取文件夹中的文件夹
                foreach (DirectoryInfo dd in dirs)
                {
                    DeleteFills(dd);        //通过delete方法删除文件夹及里面的文件
                }
                drinfo.Delete();//最后删除删除文件夹
            }

            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        
    }

}



posted @ 2012-10-31 09:11  shouqiang Wei  阅读(154)  评论(0编辑  收藏  举报