c#常用的檔案操作
c#寫入
/讀出文字檔案
public
void
page_load(object src,eventargs e)
開啟文字檔案
streamreader sr ="
.")+
"//mytext.txt
");
stringbuilder output
=new
stringbuilder();
string
rl;
while
((rl
=sr.readline())
!=null+"
");
} lblfile.text
=output.tostring();
sr.close();
c#追加檔案
streamwriter sw ="
.")+
"//mytext.txt
");
sw.writeline(
"追逐理想
");
sw.writeline(
"kzlll
");
sw.writeline(
".net筆記
");
sw.flush();
sw.close();
c#拷貝檔案
string
orignfile,newfile;
orignfile ="
.")+
"//mytext.txt
";
newfile ="
.")+
"//mytextcopy.txt
";
file.copy(orignfile,newfile,
true
);
c#刪除檔案
string
delfile ="
.")+
"//mytextcopy.txt
";
file.delete(delfile);
c#移動檔案
string
orignfile,newfile;
orignfile ="
.")+
"//mytext.txt
";
newfile ="
.")+
"//mytextcopy.txt
";
file.move(orignfile,newfile);
c#建立目錄
//建立目錄c:/sixage
directoryinfo d
=directory.createdirectory(
"c://sixage
");
//d1指向c:/sixage/sixage1
directoryinfo d1
=d.createsubdirectory(
"sixage1
");
//d2指向c:/sixage/sixage1/sixage1_1
directoryinfo d2
=d1.createsubdirectory(
"sixage1_1
");
//將當前目錄設為c:/sixage
directory.setcurrentdirectory(
"c://sixage
");
//建立目錄c:/sixage/sixage2
directory.createdirectory(
"sixage2
");
//建立目錄c:/sixage/sixage2/sixage2_1
directory.createdirectory(
"sixage2//sixage2_1
");
遞迴刪除資料夾及檔案
<%
@ page language=c#
%>
<%
@ import
namespace="
system.io
"%>
<
script runat
=server
>
public
void
deletefolder(
string
dir)
directory.delete(dir);
//刪除已空資料夾
response.write(dir+"
資料夾刪除成功
");
} else
response.write(dir+"
該資料夾不存在
");
//如果資料夾不存在則提示
} protected
void
page_load (object sender ,eventargs e)
script
>
C檔案操作常用函式
函式名 fseek 功 能 重定位流上的檔案指標 用 法 int fseek file stream,long offset,int fromwhere 程式例 include long filesize file stream int main void long filesize file st...
C 檔案操作常用函式
c 檔案操作常用函式 fopen fclose ftell fread fseek fopen 功能 開啟檔案 定義函式 file fopen const char path,const char mode 輸入引數 path字串包含欲開啟的檔案路徑及檔名 mode字串則代表著流模式。常見模式為r ...
C語言 常用檔案操作
1.fprintf 功能 傳送格式化輸出到乙個檔案中 函式原型 int fprintf file stream,char format argument,2.fscanf 功能 從乙個流中執行格式化輸入 函式原型 int fscanf file stream,char format argument...