日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#中如何创建文件夹

發布時間:2023/12/18 C# 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#中如何创建文件夹 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


C#中對文件夾操作需要用到Directory Class。其中提供了創建、刪除、移動、枚舉等靜態方法。該類不能被繼承。

?

以下代碼實現了創建文件夾。

?
1 2 3 4 if (!Directory.Exists(sPath)) { ?????Directory.CreateDirectory(sPath); }

?

以下是MSDN上Directory Class的Sample code。

http://msdn.microsoft.com/en-us/library/system.io.directory.aspx

?

以下代碼首先檢查指定的文件夾是否存在,若存在則刪除之,否則創建之。接下來移動文件夾,在其中創建文件并統計文件夾中文件數目。

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 using System; using System.IO; class Test { ????public static void Main() ????{ ????????// Specify the directories you want to manipulate. ????????string path = @"c:\MyDir"; ????????string target = @"c:\TestDir"; ????????try ????????{ ????????????// Determine whether the directory exists. ????????????if (!Directory.Exists(path)) ????????????{ ????????????????// Create the directory it does not exist. ????????????????Directory.CreateDirectory(path); ????????????} ????????????if (Directory.Exists(target)) ????????????{ ????????????????// Delete the target to ensure it is not there. ????????????????Directory.Delete(target, true); ????????????} ????????????// Move the directory. ????????????Directory.Move(path, target); ????????????// Create a file in the directory. ????????????File.CreateText(target + @"\myfile.txt"); ????????????// Count the files in the target directory. ????????????Console.WriteLine("The number of files in {0} is {1}", ????????????????target, Directory.GetFiles(target).Length); ????????} ????????catch (Exception e) ????????{ ????????????Console.WriteLine("The process failed: {0}", e.ToString()); ????????} ????????finally {} ????} }

以下代碼演示了如何計算文件夾大小。

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 // The following example calculates the size of a directory // and its subdirectories, if any, and displays the total size // in bytes. using System; using System.IO; public class ShowDirSize { ????public static long DirSize(DirectoryInfo d) ????{??? ????????long Size = 0;??? ????????// Add file sizes. ????????FileInfo[] fis = d.GetFiles(); ????????foreach (FileInfo fi in fis) ????????{????? ????????????Size += fi.Length;??? ????????} ????????// Add subdirectory sizes. ????????DirectoryInfo[] dis = d.GetDirectories(); ????????foreach (DirectoryInfo di in dis) ????????{ ????????????Size += DirSize(di);?? ????????} ????????return(Size);? ????} ????public static void Main(string[] args) ????{ ????????if (args.Length != 1) ????????{ ????????????Console.WriteLine("You must provide a directory argument at the command line.");??? ????????} ????????else ????????{? ????????????DirectoryInfo d = new DirectoryInfo(args[0]); ????????????long dsize = DirSize(d); ????????????Console.WriteLine("The size of {0} and its subdirectories is {1} bytes.", d, dsize); ????????} ????} }

總結

以上是生活随笔為你收集整理的C#中如何创建文件夹的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。