1.创建文件夹
Directory类和DirectoryInfo类实现了创建、删除、移动文件夹的方法,其中,Diretory类提供的是静态方法,DirectoryInfo类是实例类,创建文件夹的方法是CreatDiretory()方法,该方法用于创建指定路径中的所有目录
2.遍历文件夹
DiretoryInfo类提供的GetDiretories方法可以实现
public DirectoryInfo[] GetDiretories(string searchPattern,SearchOption searchOption);
SearchOption可以是TopDiretoryOnly仅在搜索中包括当前目录;AllDiretories在搜索操作中包括当前目录和所有子目录
示例代码:
DirectoryInfo theFolder = new DirectoryInfo(@"F:\a\");
DirectoryInfo[] dirInfo = theFolder.GetDirectories();
//遍历文件夹
foreach (DirectoryInfo NextFolder in dirInfo)
{
// this.listBox1.Items.Add(NextFolder.Name);
FileInfo[] fileInfo = NextFolder.GetFiles();
foreach (FileInfo NextFile in fileInfo) //遍历文件
this.listBox2.Items.Add(NextFile.Name);
}
3.删除文件夹
可以调用Diretory类的Delete方法
public void Delete()
public void Delete(bool recursive)
4.创建文件
应用File类的
public static bool Exists(string path);
public static FileStream Create(string path);创建或覆盖文件
5.修改文件属性
使用FileInfo类
6.写入文件
FileStream类用于对文件执行读写操作
Encoding.GetBytes()方法将所有编码为一个字节序列
7.修改文件名
File.Move方法能将指定文件移动到指定的位置上,并提供新的文件名
DiretoryInfo.GetFiles方法获取当前目录的文件列表
8.移动使用中的文件
需调用Windows提供的API函数MoveFileEx()方法,位于Kernel.dll文件下
9.文件搜索
FileSystemInfo类:可以表示文件或目录,可以作为FileInfo或DiretoryInfo的基础
FileAttributes枚举:提供与文件和目录的属性
10.ini文件
ini文件可以分为几个Section,每个Section的名称用[]括起来,在一个Section中,可以有很多人Key,每个Key可以有一个值并占有一行,格式Key=value
需要调用Windows提供的API
GetPrivateProfileString(),从ini文件的某个Section取得一个key的字符串
WritePrivatePRofileString(),将一个key值写入ini文件的指定Section中
11.Path
对于path使用Path.combine()替代字符串连接符(+),因为这样可以使用一些如Users\UserName之类的系统变量。
string path = "c:\\dir1\\dir2\\foo.txt"
string fullPath = "getFullPath:"+Path.GetFullPath(path);//c:\dir1\dir2\foo.txt
string dirName = "getDirectoryName:"+Path.GetDirectoryName(path);//c:\dir1\dir2
string fileName = "getFileName:"+Path.GetFileName(path);//foo.txt
string fileNameWithoutExt = "getFileNameWithoutExtension:"+Path.GetFileNameWithoutExtension(path);//foo
string extension = "getExtension:"+Path.GetExtension(path);//.txt
string pathRoot = "getPathRoot:"+Path.GetPathRoot(path);//c:\
Path.DirectorySeparatorChar
在Unix是/,在Windows上和Macintosh上是\。
12.BinaryReader,BinaryWriter
二进数据读写
public BinaryReader(Stream input);
public BinaryReader(Stream input,Encoding encoding);
input:提供的流
encoding:字符编码
常用方法及描述:
Close | 关闭当前阅读器及基础流 |
Dispose | 释放由BinaryReader占用的非托管资源,还可以另外再释放托管资源 |
PeekChar | 返回下一个可用的字符,并且不提升字节或字符的位置 |
Read | 从基础流中读取字符,并提升流的当前置 |
ReadBoolean | |
ReadByte | |
ReadBytes | 将count个字节读入字节数组 |
ReadChar | 根据当前的Encoding从当前流中读取下一个字符, |
ReadChars | |
ReadDecimal | |
ReadDouble | |
ReadInt16 | |
ReadInt32 | |
ReadInt64 | |
ReadSByte | 读取1个有符号字节 |
ReadSingle | 读取4字节符点数 |
ReadString |
BinaryWirter常用方法及描述:
Close | 关闭当前的Binary Writer和基础流 |
Dispose | 释放由BinaryWriter占用的非托管资源,还可以另外再释放托管资源 |
Flush | 清理当前编写器的所有缓冲区,使所有缓冲数据写入基础设备 |
Seek | 设置当前流中的位置 |
Write | 将值写入当前流 |
13.CryptoStream
将数据流连接到加密转换的流。
public CryptoStream(Stream stream,ICryptoTransform transfom,CryptoStreamMode mode);
stream:对其执行加密转换的流
transform:要对流执行的加密转换。
mode:CrytoStreamMode枚举值之一。
14.FileStream
FileStream类公开以文件为主的Stream,它表示在磁盘或网络路径上指向文件的流。一个FileStream类的实例实际上代表一个磁盘文件,它通过Seek方法进行对文件的随机访问,也同时包含了流的标准输入、输出和标准错误等。
15.MemoryStream
MemoryStream类用于支持存储区为内存的流。
16.NetworkStream
NetworkSteam类提供用于网络访问的基础数据流。
17.StreamReader,StreamWriter
以一种特定的编码从字节流中读写字符
18.XmlReader,XmlWriter
用于读取xml数据
最后欢迎大家访问我的个人网站: 1024s