日本好好热aⅴ|国产99视频精品免费观看|日本成人aV在线|久热香蕉国产在线

  • <cite id="ikgdy"><table id="ikgdy"></table></cite>
    1. 西西軟件下載最安全的下載網站、值得信賴的軟件下載站!

      首頁編程開發(fā)C#.NET → C# 中文件與文件夾的刪除、移動與復制實現(xiàn)代碼

      C# 中文件與文件夾的刪除、移動與復制實現(xiàn)代碼

      相關軟件相關文章發(fā)表評論 來源:百度搜索時間:2012/11/14 8:27:32字體大。A-A+

      作者:西西點擊:0次評論:0次標簽: 文件夾

      • 類型:文件處理大小:1.6M語言:中文 評分:5.0
      • 標簽:
      立即下載

      在.Net中,對文件(File)和文件夾(Folder)的操作可以使用File類和Directory類,也可以使用FileInfo類和DirectoryInfo類。文件夾(Folder)是只在Windows操作系統(tǒng)中使用的名詞。在操作系統(tǒng)的理論中,人們更習慣于使用目錄(Directory)這個名詞。或許微軟為了有朝一日將.Net移植到其他的操作系統(tǒng)中(實際上也有很多人也在做著這個項目),所以還是以Directory來命名操作文件夾的類。

      File類和Directory類都是靜態(tài)類。使用它們的好處是不需要初始化對象。如果你對某一個文件或文件夾只進行一次操作,那你最好使用該靜態(tài)類的靜態(tài)方法,比如File.Move,F(xiàn)ile.Delete等等。如果你需要對一個文件或文件夾進行多次操作,那最好還是使用FileInfo和DirectoryInfo類。因為File類和Directory是靜態(tài)類,所以你每次對一個文件或文件夾進行操作之前,它們都需要對該文件或文件夾進行一些檢查,比如authentication。如果使用FileInfo類和DirectoryInfo類,只在初始化類的對象時進行相關的檢查工作,也就是說只需要做一次,所以如果你需要對某個文件或文件夾進行多次操作,那最好使用FileInfo類和DirectoryInfo類。

      下面的這段代碼演示了如何獲得文件夾的信息,包括獲得文件夾下的子文件夾,以及文件夾下的文件。這里使用了DirectoryInfo 類來完成,當然你也可以使用Directory靜態(tài)類。

      void DisplayFolder() 
      { 
          string folderFullName = @"c:\temp"; 
          DirectoryInfo theFolder = new DirectoryInfo(folderFullName);            
          if (!theFolder.Exists) 
              throw new DirectoryNotFoundException("Folder not found: " + folderFullName);
      
          // list all subfolders in folder 
          Console.WriteLine("Subfolders:"); 
          foreach (DirectoryInfo subFolder in theFolder.GetDirectories()) 
          { 
              Console.WriteLine(subFolder.Name);                
          }
      
          // list all files in folder 
          Console.WriteLine(); 
          Console.WriteLine("Files:"); 
          foreach (FileInfo file in theFolder.GetFiles()) 
          { 
              Console.WriteLine(file.Name); 
          }                
      }

      下面演示了如何使用FileInfo類來獲得文件的相關信息,包括文件的創(chuàng)建日期,文件的大小等等。當然你同樣也可以使用File靜態(tài)類來完成。

      void DisplayFileInfo() 
      { 
          string folderFullName = @"c:\temp";           
          string fileName = "New Text Document.txt"; 
          string fileFullName = Path.Combine(folderFullName, fileName); 
          FileInfo theFile = new FileInfo(fileFullName); 
          if (!theFile.Exists) 
              throw new FileNotFoundException("File not found: " + fileFullName); 
          Console.WriteLine(string.Format("Creation time: {0}", theFile.CreationTime.ToString())); 
          Console.WriteLine(string.Format("Size: {0} bytes", theFile.Length.ToString()));            
      }

      下面的代碼分別使用了File類和FileInfo類來演示如何刪除文件

      void DeleteFile1()
      {
          string fileToBeDeleted = @"c:\temp\New Text~ Document (3).txt";
          if (File.Exists(fileToBeDeleted))
          {
              File.Delete(fileToBeDeleted);
          }
      }

      void DeleteFile2()
      {
          string fileToBeDeleted = @"c:\temp\New Text~ Document (3).txt";
          FileInfo file = new FileInfo(fileToBeDeleted);
          if (file.Exists)
          {
              file.Delete();
          }
      }

      下面的代碼分別使用了Directory類和DirectoryInfo類來演示如何刪除文件夾

      void DeleteFolder1()
      {
          string folderToBeDeleted = @"c:\temp\test";
          if (Directory.Exists(folderToBeDeleted))
          {
              // true is recursive delete:               
              Directory.Delete(folderToBeDeleted, true);
          }
      }


      void DeleteFolder2()
      {
          string folderToBeDeleted = @"c:\temp\test";
          DirectoryInfo folder = new DirectoryInfo(folderToBeDeleted);
          if (folder.Exists)
          {
              folder.Delete(true);
          }
      }

      下面的代碼分別使用了File類和FileInfo類來演示如何移動文件

      void MoveFile1()
      {
          string fileToMove = @"c:\temp\New Text Document.txt";
          string fileNewDestination = @"c:\temp\test.txt";
          if (File.Exists(fileToMove) && !File.Exists(fileNewDestination))
          {
              File.Move(fileToMove, fileNewDestination);
          }
      }



      void MoveFile2()
      {
          string fileToMove = @"c:\temp\New Text Document.txt";
          string fileNewDestination = @"c:\temp\test.txt";
          FileInfo file = new FileInfo(fileToMove);
          if (file.Exists)
          {
              file.MoveTo(fileNewDestination);
          }
      }

      下面的代碼分別使用了Directory類和DirectoryInfo類來演示如何移動文件夾

      void MoveFolder1()
      {
          string folderToMove = @"c:\temp\test";
          string folderNewDestination = @"c:\temp\test2";
          if (Directory.Exists(folderToMove))
          {
              Directory.Move(folderToMove, folderNewDestination);
          }
      }


      void MoveFolder2()
      {
          string folderToMove = @"c:\temp\test";
          string folderNewDestination = @"c:\temp\test2";
          DirectoryInfo folder = new DirectoryInfo(folderToMove);
          if (folder.Exists)
          {
              folder.MoveTo(folderNewDestination);
          }
      }

      下面的代碼分別使用了File類和FileInfo類來演示如何復制文件

      void CopyFile1()
      {
          string sourceFile = @"c:\temp\New Text Document.txt";
          string destinationFile = @"c:\temp\test.txt";
          if (File.Exists(sourceFile))
          {
              // true is overwrite
              File.Copy(sourceFile, destinationFile, true);
          }
      }


      void CopyFile2()
      {
          string sourceFile = @"c:\temp\New Text Document.txt";
          string destinationFile = @"c:\temp\test.txt";
          FileInfo file = new FileInfo(sourceFile);
          if (file.Exists)
          {
              // true is overwrite
              file.CopyTo(destinationFile, true);
          }
      }

        相關評論

        閱讀本文后您有什么感想? 已有人給出評價!

        • 8 喜歡喜歡
        • 3 頂
        • 1 難過難過
        • 5 囧
        • 3 圍觀圍觀
        • 2 無聊無聊

        熱門評論

        最新評論

        發(fā)表評論 查看所有評論(0)

        昵稱:
        表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
        字數(shù): 0/500 (您的評論需要經過審核才能顯示)