• 获取当前文件夹下的所有空文件夹路径信息


                                                    获取当前文件夹下的所有空文件夹路径信息
      1using System;
      2using System.Drawing;
      3using System.Collections;
      4using System.ComponentModel;
      5using System.Windows.Forms;
      6using System.Data;
      7using System.IO;
      8
      9namespace 删除空文件夹
     10{
     11    /// <summary>
     12    /// Form1 的摘要说明。
     13    /// </summary>

     14    public class Form1 : System.Windows.Forms.Form
     15    {
     16        private System.Windows.Forms.Button button1;
     17        private System.Windows.Forms.RichTextBox richTextBox1;
     18        /// <summary>
     19        /// 必需的设计器变量。
     20        /// </summary>

     21        private System.ComponentModel.Container components = null;
     22
     23        public Form1()
     24        {
     25            //
     26            // Windows 窗体设计器支持所必需的
     27            //
     28            InitializeComponent();
     29
     30            //
     31            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
     32            //
     33        }

     34
     35        /// <summary>
     36        /// 清理所有正在使用的资源。
     37        /// </summary>

     38        protected override void Dispose( bool disposing )
     39        {
     40            if( disposing )
     41            {
     42                if (components != null
     43                {
     44                    components.Dispose();
     45                }

     46            }

     47            base.Dispose( disposing );
     48        }

     49
     50        Windows 窗体设计器生成的代码
     94
     95        /// <summary>
     96        /// 应用程序的主入口点。
     97        /// </summary>

     98        [STAThread]
     99        static void Main() 
    100        {
    101            Application.Run(new Form1());
    102        }

    103        //遍历当前文件夹下的空文件夹(包括它的下一级文件夹)
    104        private void SearchDirectoryEmptyFolder(string  targetDirectory ) 
    105        {
    106            if (Directory.Exists(targetDirectory))
    107            {
    108                DirectoryInfo di = new DirectoryInfo(targetDirectory);
    109                if( DirSize(di)== 0)
    110                {
    111
    112                    //    ' Empty folder 
    113                    w1++;
    114                    this.richTextBox1.Text +=w1.ToString()+  ("Folder " + targetDirectory +" is empty\n") ;
    115                }

    116                else
    117                {
    118                    //' Recurse subdirectories of this directory 
    119                    //string  subdirectory ="";
    120                    string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory) ;
    121                    foreachstring subdirectory in subdirectoryEntries )
    122                    {
    123                        //this.richTextBox1.Text +=("Folder is " + subdirectory+"\n") ;
    124                        SearchDirectoryEmptyFolder(subdirectory) ;
    125                    }

    126                }

    127            }

    128        }

    129        //遍历当前文件夹下的所有文件信息(包括它的下一级文件夹)
    130        public static void DirSearch(DirectoryInfo d) 
    131        {
    132            //get the current directory files information
    133            FileInfo[] files =  d.GetFiles();
    134            Console.WriteLine("Total number of txt files:{0}",files.Length);
    135            foreach(FileInfo f in files)
    136            {
    137                Console.WriteLine("Name is : {0}", f.Name);
    138                Console.WriteLine("Length of the file is : {0}", f.Length);
    139                Console.WriteLine("Creation time is : {0}", f.CreationTime);
    140                Console.WriteLine("the path of the file: {0}",f.FullName);
    141                Console.WriteLine("Attributes of the file are : {0}",
    142                    f.Attributes.ToString());
    143            }

    144            
    145            // recursion transfer get the next directory information
    146            DirectoryInfo[] dis = d.GetDirectories();
    147            foreach (DirectoryInfo di in dis)
    148            {
    149                DirSearch(di);
    150            }

    151        }

    152
    153
    154        int w1=0;
    155        //遍历当前文件夹下的所有文件夹名(包括它的下一级文件夹))
    156        private void SearchDirectoryFolder(string  targetDirectory ) 
    157        {
    158            if (Directory.Exists(targetDirectory))
    159            {
    160                if( Directory.GetFileSystemEntries(targetDirectory).Length == 0)
    161                {
    162                    //Empty folder 
    163                    w1++;
    164                    this.richTextBox1.Text +=w1.ToString()+  ("Folder " + targetDirectory +" is empty\n") ;
    165                }

    166                else
    167                {
    168                    //' Recurse subdirectories of this directory 
    169                    string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory) ;
    170                    foreachstring subdirectory in subdirectoryEntries )
    171                    {
    172                        //this.richTextBox1.Text +=("Folder is " + subdirectory+"\n") ;
    173                        SearchDirectoryFolder(subdirectory) ;
    174                    }

    175                }

    176            }

    177        }

    178
    179        private void button1_Click(object sender, System.EventArgs e)
    180        {
    181            this.richTextBox1.Text =""
    182    
    183            //SearchDirectoryFolder(@"c:\CarArchives");
    184            //DirectoryInfo di = new DirectoryInfo(@"c:\CarArchives");
    185            //long i=DirSize(di)/1024;
    186            //this.richTextBox1.Text = (i.ToString()+"KB" ) ;
    187            //SearchDirectoryEmptyFolder(@"c:\CarArchives");
    188            //ProcessDirectory(@"c:\CarArchives");
    189            PrintFileSystemEntries(@"c:\CarArchives\2005");
    190        }

    191
    192        
    193
    194        //计算当前文件夹的大小(包括它的下一级文件夹)
    195        public  long DirSize(DirectoryInfo d) 
    196        {    
    197            long Size = 0;    
    198            // Add file sizes.
    199            FileInfo[] fis = d.GetFiles();
    200            foreach (FileInfo fi in fis) 
    201            {      
    202                Size +=fi.Length; ;
    203            }

    204            // Add subdirectory sizes.
    205            DirectoryInfo[] dis = d.GetDirectories();
    206            foreach (DirectoryInfo di in dis) 
    207            {
    208                Size +=DirSize(di);  ;
    209            }

    210            return(Size);  
    211        }

    212
    213        //获取当前文件夹中的所有文件和文件夹(不包括它的下一级目录)
    214        void PrintFileSystemEntries(string path) 
    215        {
    216            
    217            try 
    218            {
    219                // Obtain the file system entries in the directory path.
    220                string[] directoryEntries =
    221                    System.IO.Directory.GetFileSystemEntries(path); 
    222
    223                foreach (string str in directoryEntries) 
    224                {
    225                    //System.Console.WriteLine(str);
    226                    this.richTextBox1.Text+=str+"\n"  ;
    227                }

    228            }

    229            catch (ArgumentNullException) 
    230            {
    231                System.Console.WriteLine("Path is a null reference.");
    232            }

    233            catch (System.Security.SecurityException) 
    234            {
    235                System.Console.WriteLine("The caller does not have the " +
    236                    "required permission.");
    237            }

    238            catch (ArgumentException) 
    239            {
    240                System.Console.WriteLine("Path is an empty string, " +
    241                    "contains only white spaces, " + 
    242                    "or contains invalid characters.");
    243            }

    244            catch (System.IO.DirectoryNotFoundException) 
    245            {
    246                System.Console.WriteLine("The path encapsulated in the " + 
    247                    "Directory object does not exist.");
    248            }

    249        }

    250
    251        //获取当前文件夹下的所有文件和文件夹(不包括它的下一级目录)
    252        public  void ProcessDirectory(string targetDirectory) 
    253        {
    254            // Process the list of files found in the directory.
    255            string [] fileEntries = Directory.GetFiles(targetDirectory);
    256            foreach(string fileName in fileEntries)
    257                this.richTextBox1.Text +=fileName+"\n";
    258            // Recurse into subdirectories of this directory.
    259            string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
    260            foreach(string subdirectory in subdirectoryEntries)
    261            {
    262                this.richTextBox1.Text += subdirectory+"\n"+"**********************************\n\n"    ;
    263                ProcessDirectory(subdirectory);
    264            }

    265
    266            
    267        }

    268
    269    }

    270
    271
    272}

    273
  • 相关阅读:
    jwt
    mybatis的回顾
    swagger
    MySQl总结
    Java异常
    常用Dos命令
    C++初级项目——机房预约系统
    C++中将数字型字符串转变为int类型的方法
    C++中int *a; int &a; int & *a; int * &a
    #define_CRT_SECURE_NO_WARNINGS的用法
  • 原文地址:https://www.cnblogs.com/furenjun/p/292298.html
Copyright © 2020-2023  润新知