1.创建文件夹 //using System.IO; Directory.CreateDirectory(%%1);
2.创建文件 //using System.IO; File.Create(%%1);
3.删除文件 //using System.IO; File.Delete(%%1);
4.删除文件夹 //using System.IO; Directory.Delete(%%1);
5.删除一个目录下所有的文件夹
//using System.IO;
foreach (string dirStr in Directory.GetDirectories(%%1))
{
DirectoryInfo dir = new DirectoryInfo(dirStr);
ArrayList folders=new ArrayList();
FileSystemInfo[] fileArr = dir.GetFileSystemInfos();
for (int i = 0; i < folders.Count; i++)
{
FileInfo f = folders[i] as FileInfo;
if (f == null)
{
DirectoryInfo d = folders[i] as DirectoryInfo;
d.Delete();
}
}
}
6.清空文件夹
//using System.IO;
Directory.Delete(%%1,true); Directory.CreateDirectory(%%1);
7.读取文件
//using System.IO;
StreamReader s = File.OpenText(%%1);
string %%2 = null;
while ((%%2 = s.ReadLine()) != null){
%%3
}
s.Close();
8.写入文件
//using System.IO;
FileInfo f = new FileInfo(%%1);
treamWriter w = f.CreateText();
w.WriteLine(%%2); w.Close();
9.写入随机文件
//using System.IO;
byte[] dataArray = new byte[100000];//new Random().NextBytes(dataArray);
using(FileStream FileStream = new FileStream(%%1, FileMode.Create)){
// 一个字节一个字节地将数据写入文件。
for(int i = 0; i < dataArray.Length; i++){
FileStream.WriteByte(dataArray[i]);
}
// 将流位置设置为文件的开头。
FileStream.Seek(0, SeekOrigin.Begin);
// 阅读并验证数据。
for(int i = 0; i < FileStream.Length; i++){
if(dataArray[i] != FileStream.ReadByte()){
//写入数据错误
return;
}
}
//“数据流”+FileStream.Name+“已验证”
}
10.读取文件属性
//using System.IO;
FileInfo f = new FileInfo(%%1);//f.CreationTime,f.FullName
if((f.Attributes & FileAttributes.ReadOnly) != 0){
%%2
} else{
%%3
}