C 檔案 資料夾特別操作

2021-09-08 18:15:36 字數 3015 閱讀 6780

1、過濾特殊字元

public

class

charservice:idisposable

//////

特殊字元過濾

/// ///

///public

string splitstringinvaild(string str,bool islower=true

)

str =sb.tostring().trim();

if(islower)

}return

str;

}public

static

char tolower(char

c)

return

c; }

public

static

string todbc(string input)

if (c[i] > 65280 && c[i] < 65375

) c[i] = (char)(c[i] - 65248

); }

return

newstring(c);

}public

void

dispose()

}

view code

2、將datatable資料儲存為.csv檔案

private

bool datatabletocsv(system.data.datatable tb, string

filename)

text = string.join(","

, fields);

writer.writeline(text);

//遍歷行

foreach (datarow row in

tb.rows)

if (row[i] ==dbnull.value)

string field =row[i].tostring();

//csv格式有規定,比如,如果欄位中有逗號、雙引號、回車換行符、前或後置空格,

//必須把字段用雙引號擴起來,如果文字中有雙引號,必須用兩個雙引號替換

field = field.replace("

\"", "

\"\"");

if (field.indexof('

,') >= 0

) \"

", field);

continue

; }

if (field.indexof('

\r') >= 0

) \"

", field);

continue

; }

if (field.indexof('

\n') >= 0

) \"

", field);

continue

; }

if (field.indexof('

\"') >= 0

) \"

", field);

continue

; }

if (field !=field.trim())

\"", field);

continue

; }

fields[i] = (i == 3) ? datetime.parse(row[i].tostring()).tostring("

yyyy/mm/dd hh:mm:ss

") : row[i].tostring();

}text = string.join(","

, fields);

writer.writeline(text);

}_logserv.debug(

"匯出成功");

"匯出成功", "提示");

export = true

; }

catch

(exception ex)

finally

}return

export;

}

view code

3、+過濾掉非法字元

///

///過濾掉非法字元和點字元

/// ///

///public

string directorynamefilter(string directoryname)

return

directoryname;

}//////

過濾掉非法字元

/// ///

///public

string namefilter(string name)

return

name;

}

view code

4、計算資料夾大小

#region 計算資料夾大小

//////計算資料夾大小

/// ///

資料夾 ///

public

long calculatedirsize(string

dirpath)

directoryinfo dirs =dirinfo.getdirectories();

foreach (directoryinfo dinfo in

dirs)}}

catch

(exception ex)

return

sumsize;

}

view code

C 資料夾操作

去除字尾 獲取string最後乙個乙個字元 void main 讀取資料夾中所有檔名 include include include 忘了這個cout就無法輸出string型別。include include finddata t,findfirst,findnext,findclose要用 usi...

C 資料夾操作

author ricard.m.tan createtime 2020 4 2 11 52 41 using system.io using system.windows.forms using system.xml namespace chngz.paradise 複製檔案 public stat...

C 資料夾操作

一 資料夾操作 directory類,directoryinfo類.使用using system.io命名空間 一 建立資料夾 方法一 1 private string path f text ceshi 2 private void create click object sender,event...