1.path
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace path { class Program { static void Main(string[] args) { //Path类专门用来处理路径的! string str = @"C: muninst.ini"; //获取文件名 Console.WriteLine(Path.GetFileName(str)); //获取文件名,但不包括扩展名 Console.WriteLine(Path.GetFileNameWithoutExtension(str)); //获得文件扩展名 Console.WriteLine(Path.GetExtension(str)); //获得文件所在的文件夹的名称 Console.WriteLine(Path.GetDirectoryName(str)); //获取文件的全路径 Console.WriteLine(Path.GetFullPath(str)); //连接两个字符串作为路径 Console.WriteLine(Path.Combine(@"c:","c.txt")); Console.ReadKey(); } } }
2.file类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace file { class Program { static void Main(string[] args) { //创建文件 //File.Create(@"C:Usersalexx.zhangDesktop123456789.txt"); //删除 File.Delete(@"C:Usersalexx.zhangDesktop123456789.txt"); //复制 //File.Copy(@"",@""); //判断文件是否存在 if(!File.Exists(@"C:Usersalexx.zhangDesktop123456789.txt")) { File.Create(@"C:Usersalexx.zhangDesktop123456789.txt"); Console.WriteLine("OK"); } else { Console.WriteLine("Exists"); } Console.ReadKey(); } } }
**ReadAllBytes/WriteAllBytes
//读 ReadAllBytes byte[] buffer= File.ReadAllBytes(@"C:Usersalexx.zhangDesktop123456789.txt"); //将字节数组中的每个元素都要按照我们指定的编码格式解码成字符串 //UTF-8 GB2312K GBK Unicode string s = Encoding.GetEncoding("GBK").GetString(buffer); //string ss = Encoding.Default.GetString(buffer); Console.WriteLine(s); //写 WriteAllBytes string str = "凄凄切切"; byte[] buffer1 = Encoding.GetEncoding("GBK").GetBytes(str); File.WriteAllBytes(@"C:Usersalexx.zhangDesktop123456789.txt",buffer1); Console.ReadKey();
**File.ReadAllLines
string [] array= File.ReadAllLines(@"C:Usersalexx.zhangDesktop123456789.txt", Encoding.GetEncoding("GBK")); foreach (var item in array) { Console.WriteLine(item); }
**File.ReadAllText
string str= File.ReadAllText(@"C:Usersalexx.zhangDesktop123456789.txt", Encoding.GetEncoding("GBK")); Console.WriteLine(str);
**File.WriteAllLines/File.WriteAllText
string [] strs={"123","234","345"}; File.WriteAllLines(@"C:Usersalexx.zhangDesktop123456789.txt", strs); string str="1234567890"; File.WriteAllText(@"C:Usersalexx.zhangDesktop1234567890.txt", str);
**File.AppendAllLines
**File.AppendAllText
**File.AppendText
****File类只能读取小文件!,因为它是一下子读取,占内存
3.编码
4.FileStream文件流
1)FileStream 操作字节的
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace FileStream { class Program { static void Main(string[] args) { //FileStream 操作字节的 //***读取数据 //构造函数:路径,针对文件的操作,针对文件的数据的操作 System.IO.FileStream fs = new System.IO.FileStream(@"C:Usersalexx.zhangDesktop123.txt", FileMode.OpenOrCreate,FileAccess.Read); byte[] buffer = new byte[1024 * 1024 * 5]; //返回本次实际读取到的有效字节数 int i= fs.Read(buffer, 0, buffer.Length); //将字节数组中每个元素按照指定的编码格式编译成字符 string s = Encoding.Default.GetString(buffer,0,i); //关闭流 fs.Close(); //释放流所占用的资源 fs.Dispose(); Console.WriteLine(s); //***写入数据 using (System.IO.FileStream fss = new System.IO.FileStream(@"C:Usersalexx.zhangDesktop123.txt", FileMode.OpenOrCreate, FileAccess.Write)) { string str = "898989898985222232255002..22256655"; byte[] buffers = Encoding.Default.GetBytes(str); fss.Write(buffers, 0, buffers.Length); } Console.ReadKey(); } } }
2)StreamReader,StreamWriter 操作字符的