⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex-21-03

📁 Programming Csharp Source Code(代码) Programming Csharp Source Code
💻
字号:
// Example 21-03: Creating a subdirectory and manipulating files

namespace Programming_CSharp
{
   using System;
   using System.IO;

   class Tester
   {
      public static void Main()
      {
         // make an instance and run it
         Tester t = new Tester();
         string theDirectory = @"c:\test\media";
         DirectoryInfo dir = new DirectoryInfo(theDirectory);
         t.ExploreDirectory(dir);
      }
        
      // Set it running with a directory name
      private void ExploreDirectory(DirectoryInfo dir)
      {

         // make a new subdirectory
         string newDirectory = "newTest";
         DirectoryInfo newSubDir = 
            dir.CreateSubdirectory(newDirectory);

         // get all the files in the directory and
         // copy them to the new directory
         FileInfo[] filesInDir = dir.GetFiles();
         foreach (FileInfo file in filesInDir)
         {
            string fullName = newSubDir.FullName +  
               "\\" + file.Name;
            file.CopyTo(fullName);
            Console.WriteLine("{0} copied to newTest", 
               file.FullName);
         }

         // get a collection of the files copied in
         filesInDir = newSubDir.GetFiles();

         // delete some and rename others
         int counter = 0;
         foreach (FileInfo file in filesInDir)
         {
            string fullName = file.FullName;

            if (counter++ %2 == 0)
            {
               file.MoveTo(fullName + ".bak");
               Console.WriteLine("{0} renamed to {1}",
                  fullName,file.FullName);
            }
            else
            {
               file.Delete();
               Console.WriteLine("{0} deleted.", 
                  fullName);
            }
         }

         newSubDir.Delete(true); // delete the subdirectory 
      }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -