zoukankan      html  css  js  c++  java
  • [置顶] 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);
                }
            }
            
        }

    }



  • 相关阅读:
    JSON,数组根据字段多次分组
    .net c#后台请求接口
    数组的高级应用含ES6 for of 用法
    js获取当前页面url网址信息
    资源管理神器Clover
    ES6的7个实用技巧
    单标签实现气泡三角形
    移动端--上拉加载更多
    checkbox对齐-复选框图标
    textarea显示源代码
  • 原文地址:https://www.cnblogs.com/wsq724439564/p/3258221.html
Copyright © 2011-2022 走看看