c 檔案讀取,移動,複製

2021-10-09 09:58:42 字數 3256 閱讀 2939

//1.讀取path路徑下所有檔案

directoryinfo root =

newdirectoryinfo

(path)

; fileinfo[

] files = root.

getfiles()

;

//2.讀取檔案內容 fullname為檔案完整路徑 例項:e:\edi\text.txt

string str = system.io.file.

readalltext

(fullname)

;

1.將字串rpath對映為真實路徑(路徑不存在自動建立)

string newpath = system.io.path.

combine

(rpath)

; system.io.directory.

createdirectory

(newpath)

;

2.將兩個字串組合成乙個路徑。(即在rpath目錄下,新建目錄(目錄名為:新建目錄名))

string newpath = system.io.path.

combine

(rpath,新建目錄名)

; system.io.directory.

createdirectory

(newpath)

;

//將strtest 字串儲存為檔案,輸出至指定rpath路徑(rpath路徑需真實存在,示例:e:\edi\eln\結果檔名.txt)   

strtest =

"測試資訊"

;

system.io.file.

writealltext

(@rpath, strtest, encoding.utf8)

;

//移動檔案位置

//新建目錄 存放被移動檔案

string newmovepath = system.io.path.

combine

(d:\text)

; system.io.directory.

createdirectory

(newmovepath)

;//移動檔案到指定位置

//fullname:檔案原目錄 示例: d:\text

//newmovepath:移動到目錄(檔案新目錄\檔名) 示例: d:\text\text.txt

file.

move

(@fullname, @newmovepath)

;

//複製檔案到指定位置      

//fullname:檔案原目錄 示例: d:\text

//newmovepath:複製到目錄(檔案新目錄\檔名) 示例: d:\text\text.txt

file.

copy

(@fullname, @newmovepath)

;

例項:

/// 

///

///

/// 被解析檔案 所在路徑(路徑格式例項:e:\edi sample\eln)

/// 結果檔案 存放路徑(預設存放至d盤根目錄, 路徑格式例項:e:\edi sample\)

///

public

string

readfill

(string path,

string rpath =

@"d:\")

, stringsplitoptions.removeemptyentries)

;//分割後,擷取需要資料

string cs = strarray[1]

.substring(1

,13)+

":"+ strarray[1]

.substring(18

,13);

// 結果資料 拼接文字

strtest +

="目錄"

+ item.directoryname +

"下第"

+++c +

"個檔案"

+"\n"

+"檔名為:'"

+ item.fullname +

"'"+

"\n"

+"解析內容為:'"

+ cs +

"'"+

"\n"

+"\n"

+"\n"

;//移動檔案位置

新建目錄 存放被移動檔案

//string newmovepath = system.io.path.combine(item.directoryname + "_move"+ "\\"+datetime.now.tostring("yyyy-mm-dd"));

file.copy(item.fullname, newmovepath+item.name);

將檔案 移動至新資料夾下

newmovepath+"\\" + item.name);

}//結果檔案存放位置

//在指定目錄下,新建目錄

string newpath = system.io.path.

combine

(rpath,datetime.now.

tostring

("yyyy-mm-dd"))

; system.io.directory.

createdirectory

(newpath)

;//將結果輸出至指定rpath路徑

system.io.file.

writealltext

(newpath +

"\\"

+ datetime.now.

tostring

(" yyyymmddhhmmss")+

"讀取結果.txt"

, strtest, encoding.utf8)

;return

"執行成功";}

catch

(exception ex)

}

C 檔案及資料夾複製,移動,刪除

class file dirmanipulate filemove 源路徑 目標路徑 public static void filemove string srcfilepath,string destfilepath filedelete public static void filedelete...

複製,刪除,移動完整檔案

1.刪除完整檔案 包括當前檔案目錄 刪除完整目錄 傳入要刪除的資料夾 function deldir dir else rmdir dir 移除該空資料夾 deldir imgs 2.複製完整目錄 複製完整目錄 dir1 原始檔路徑 dir2目的檔案路徑 function copydir dir1,...

檔案的複製和移動

cp mv的用途是把乙個及以上的原始檔複製 移動到另乙個位置。當原始檔個數大於2時,目的檔案型別必須是已存在的目錄。原始檔型別 目的檔案型別 結果檔案 已存在的目錄 檔案被複製 移動到目的目錄 目錄已存在的目錄 目錄本身連同其內容被複製 移動到目的目錄 當原始檔個數等於1時,則可能在複製 移動檔案的...