首页 > 代码库 > Path和File

Path和File

path类
string str=@"c:\3000soft\red spider\data\message\老赵.wav";
Path.GetFileName(str);\\获取文件名
Path.GetFileNameWithoutExtension(str);//获取文件名但是不包括扩展名
Path.GetExtension(str);//获取文件的扩展名
Path.GetDirectoryName(str);//获取文件所在文件夹的名称。
Path.GetFullPath(str);//获取文件所在全路径
Path.Combine(@"c:\a\","b.txt");//连接两个字符串作为路径

File类
string str=@"c:\users\springRain\Desktop\new.txt";
//创建一个文件
File.Create(str);
//删除一个文件
File.Delete(str);
//复制一个文件
File.Copy("原文件fullpath","新文件fullPath");

读写数据
File.ReadAllBytes() 字节数组--》字符串 Encoding.Default.GetString(字节数组);
File.WriteAllBytes()字符串--》字节数组 Encoding.Default.GetBytes(字符串);


byte[] buffer=File.ReadAllBytes(@"C:\users\new.text");
//将字节数组中的每个元素都要按照我们指定的编码格式解码成字符串
//UTF-8 GB2312 GBK ASCII Unicode
string s=Encoding.GetEncoding("GB2312").GetString(buffer);

//没有这个文件的话,会给你创建一个,有的话会给你覆盖掉
string str="今天天气好晴朗处处好风光";
//需要将字符串转换成字节数组
byte[] buffer=Encoding.Default.GetBytes(str);
File.WriteAllBytes(@"c:\users\new.txt",buffer);

Path和File