国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

如何復制一個目錄里面的所有目錄和文件

2024-07-21 02:16:19
字體:
來源:轉載
供稿:網友

本文介紹如何將一個目錄里面的所有文件復制到目標目錄里面。
下面介紹幾個我們在該例程中將要使用的類:
1、directory:exposes static methods for creating, moving, and enumerating through directories and subdirectories.
2、path:performs operations on string instances that contain file or directory path information. these operations are performed in a cross-platform manner.
3、file:provides static methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of filestream objects.
這兩個類里都是不可繼承的,是從object直接繼承來的,被實現成sealed,上面的解釋均來自msdn。
下面是實現的代碼,代碼中的細節不在這里詳細描述,請看代碼注釋:
// ======================================================
// 實現一個靜態方法將指定文件夾下面的所有內容copy到目標文件夾下面
// ======================================================
public static void copydir(string srcpath,string aimpath){
// 檢查目標目錄是否以目錄分割字符結束如果不是則添加之
if(aimpath[aimpath.length-1] != path.directoryseparatorchar)
aimpath += path.directoryseparatorchar;
// 判斷目標目錄是否存在如果不存在則新建之
if(!directory.exists(aimpath)) directory.createdirectory(aimpath);
// 得到源目錄的文件列表,該里面是包含文件以及目錄路徑的一個數組
// 如果你指向copy目標文件下面的文件而不包含目錄請使用下面的方法
// string[] filelist = directory.getfiles(srcpath);
string[] filelist = directory.getfilesystementries(srcpath);
// 遍歷所有的文件和目錄
foreach(string file in filelist){
// 先當作目錄處理如果存在這個目錄就遞歸copy該目錄下面的文件
if(directory.exists(file))
copydir(file,aimpath+path.getfilename(file));
// 否則直接copy文件
else
file.copy(file,aimpath+path.getfilename(file),true);
}
}

嘿嘿!這次給大家說的功能比較簡單,適合初學者,希望對初學者有所幫助!如果你需要將該功能使用在web工程中,那么請設置給aspnet帳號足夠的權限,不過這樣做是很危險的,不建議這樣做。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 夹江县| 东安县| 西盟| 科尔| 丰台区| 徐水县| 双辽市| 平度市| 麻栗坡县| 平顺县| 揭西县| 博白县| 上蔡县| 定南县| 娱乐| 浪卡子县| 万源市| 兴国县| 龙川县| 太白县| 鹤岗市| 布拖县| 麻阳| 崇文区| 富蕴县| 马公市| 西盟| 乌苏市| 炉霍县| 高唐县| 仙游县| 营口市| 墨竹工卡县| 宁陵县| 武乡县| 平泉县| 信丰县| 孝义市| 木兰县| 长沙市| 庄河市|