2023年12月1日发(作者:)

//创建文件夹

Directory(h("a"));

Directory(h("b"));

Directory(h("c"));

//移动b到a

(h("b"), h("ab"));

//删除c

(h("c"));



//2.---------文件创建、复制、移动、删除---------



//创建文件

//使用创建再复制/移动/删除时会提示:文件正由另一进程使用,因此该进程无法访问该文件

//改用 FileStream 获取 返回的 ream 再进行关闭就无此问题

FileStream fs;

fs = (h(""));

();

fs = (h(""));

();

fs = (h(""));

();

//复制文件

(h(""), h(""));

//移动文件

(h(""), h(""));

(h(""), h(""));

//删除文件

(h(""));



//3.---------遍历文件夹中的文件和子文件夹并显示其属性---------



if((h("a")))

{

//所有子文件夹

foreach(string item in ectories(h("a")))

{

("文件夹:" + item + "
");

DirectoryInfo directoryinfo = new DirectoryInfo(item);

("名称:" + + "
");

("路径:" + me + "
");

("创建时间:" + onTime + "
");

("上次访问时间:" + cessTime + "
");

("上次修改时间:" + iteTime + "
");

("父文件夹:" + + "
");

("所在根目录:" + + "
");

("
");

}



//所有子文件

foreach (string item in es(h("a")))

{

("文件:" + item + "
");

FileInfo fileinfo = new FileInfo(item);

("名称:" + + "
");

("扩展名:" + ion +"
");

("路径:" + me +"
");

("大小:" + +"
");

("创建时间:" + onTime +"
");

("上次访问时间:" + cessTime +"
");

("上次修改时间:" + iteTime +"
");

("所在文件夹:" + oryName +"
");

("文件属性:" + utes +"
");

("
");



}

}



//4.---------文件读写---------



if ((h("")))

{

StreamWriter streamwrite = new StreamWriter(h(""));

ine("木子屋");

ine("/");

("2008-04-13");

();



StreamReader streamreader = new StreamReader(h(""));

(ne());

(End());

();

}



获取文件的版本信息:

FileVersionInfo myFileVersionInfo1 = sionInfo("D:");

="版本号: " + rsion;



更改文件属性,删除只读文件:



下例欲将E:文件拷贝至D:,但D:已经存在。



//(sourceFile,destinationFile,true); 用来拷贝文件

//当destinationFile已经存在时,无法将文件file1拷贝到目标文件,

//因此先删除destination文件,()方法不能删除只读文件,

//因此,如果文件属性为只读(Attributes属性中会包含有"ReadOnly"),

//先把文件属性重置为Normal,然后再删除:

string file1="E:";

string destinationFile="d:";

if((destinationFile))

{

FileInfo fi=new FileInfo(destinationFile);

if(ng().IndexOf("ReadOnly")!=-1)

utes=;

(destinationFile);

}

(file1,destinationFile,true);



判断文件是否存在:(string filePath)



判断目录是否存在:("D:LastestVersion")



按行读取文件:



int fileCount=0;

// Open the file just specified such that no one else can use it.

StreamReader sr = new StreamReader(());

while(() > -1)//()返回下一个可用字符,但不使用它

{

(ne());

fileCount++;

}

();



按行写入文件:

StreamWriter sw = new StreamWriter("D:
");

for(int i=0;i<10;i++)

{

ine("这是第"+ng()+"行数据");

}



C#追加文件

StreamWriter sw = Text(h(".")+"");

ine("追逐理想");

ine("kzlll");

ine(".NET笔记");

();

();



C#拷贝文件

string OrignFile,NewFile;

OrignFile = h(".")+"";

NewFile = h(".")+"";

(OrignFile,NewFile,true);



C#删除文件

string delFile = h(".")+"";

(delFile);



C#移动文件

string OrignFile,NewFile;

OrignFile = h(".")+"";

NewFile = h(".")+"";

(OrignFile,NewFile);



C#创建目录

// 创建目录c:sixAge

DirectoryInfo d=Directory("c:sixAge");



// d1指向c:sixAgesixAge1

DirectoryInfo d1=Subdirectory("sixAge1");

// d2指向c:sixAgesixAge1sixAge1_1

DirectoryInfo d2=Subdirectory("sixAge1_1");

// 将当前目录设为c:sixAge

rentDirectory("c:sixAge");

// 创建目录c:sixAgesixAge2

Directory("sixAge2");

// 创建目录c:sixAgesixAge2sixAge2_1

Directory("sixAge2sixAge2_1");



递归删除文件夹及文件

<%@ Page Language=C#%>

<%@ Import namespace=""%>