使用File、Path和Directory进行常见的操作
我们偶尔会用到文件操作,其中File、Path和Directory这三个类是比较常见的,今天写了一个测试demo,也是顺便学习一下,记录一二。 BTW,使用这几个类的时候需要引用using System.IO命名空间。 1 class Program 2 { 3 static void Main(string[] args) 4 { 5 //ProcessPath(); 6 //ProcessFile(); 7 //ReadFileByByteArray(); 8 //ReadFileByAllLines(); 9 //ReadFileByAllText(); 10 //WriteFileByByteArray(); 11 //WriteFileByLines(); 12 //WriteFileByText(); 13 UseDirectory(); 14 Console.ReadKey(); 15 } 16 /// <summary> 17 /// path类只是在string层面进行操作 18 /// </summary> 19 private static void ProcessPath() 20 { 21 string path = @"C:\Users\Administrator\Desktop\XX-Net.txt"; 22 string directory = Path.GetDirectoryName(path); 23 string fileName = Path.GetFileName(path); 24 string fileNamenoExtension = Path.GetFileNameWithoutExtension(path); 25 string fileExtension = Path.GetExtension(path); 26 string ss = Path.ChangeExtension(path, "csv"); 27 Console.WriteLine($"directory is {directory},fileName is {fileName},file without extension is{fileNamenoExtension},extension is {fileExtension},new file is{ss} "); 28 } 29 private static void ProcessFile() 30 { 31 string path = @"C:\Users\Administrator\Desktop\2.txt"; 32 //File.Create(path); 33 //File.Delete(path); 34 //File.Copy(path, @"C:\Users\Administrator\Desktop\3.txt"); 35 File.Move(path, @"C:\Users\Administrator\Desktop\4.txt"); 36 Console.WriteLine("creat successfully..."); 37 } 38 private static void ReadFileByByteArray() 39 { 40 string path = @"C:\Users\Administrator\Desktop\3.txt"; 41 byte[] fileByte = File.ReadAllBytes(path); 42 string fileByByte = Encoding.Default.GetString(fileByte); 43 Console.WriteLine(fileByByte); 44 } 45 private static void ReadFileByAllLines() 46 { 47 string path = @"C:\Users\Administrator\Desktop\3.txt"; 48 string[] fileLines = File.ReadAllLines(path,Encoding.Default); 49 foreach (var line in fileLines) 50 { 51 Console.WriteLine(line); 52 } 53 } 54 private static void ReadFileByAllText() 55 { 56 string path = @"C:\Users\Administrator\Desktop\3.txt"; 57 string fileText = File.ReadAllText(path,Encoding.Default); 58 Console.WriteLine(fileText); 59 } 60 private static void WriteFileByByteArray() 61 { 62 string path = @"C:\Users\Administrator\Desktop\1.txt"; 63 string fileStr = "大家好,我来了,我的号码是1388888888"; 64 byte[] fileByte = Encoding.Default.GetBytes(fileStr); 65 File.WriteAllBytes(path, fileByte); 66 Console.WriteLine("Write Sucessfully..."); 67 } 68 private static void WriteFileByLines() 69 { 70 string path = @"C:\Users\Administrator\Desktop\1.txt"; 71 string[] fileArray = { "你好","我的名字","叫Tom","我来自安徽省阜阳市太和县","他们","都知道我是个好人" }; 72 File.WriteAllLines(path, fileArray); 73 Console.WriteLine("Write Sucessfully..."); 74 } 75 private static void WriteFileByText() 76 { 77 string path = @"C:\Users\Administrator\Desktop\1.txt"; 78 //File.WriteAllText(path, "落霞与孤鹜齐飞,秋水共长天一色。"); 79 File.AppendAllText(path, "宜将剩勇追穷寇,不可沽名学霸王!"); 80 Console.WriteLine("Write Sucessfully..."); 81 } 82 private static void UseDirectory() 83 { 84 //Directory.CreateDirectory(@"C:\Users\Administrator\Desktop\NewFolder"); 85 //Directory.Delete(@"C:\Users\Administrator\Desktop\NewFolder",true); 86 87 string[] files = Directory.GetFiles(@"D:\迅雷下载", "*.rmvb",SearchOption.TopDirectoryOnly); 88 foreach (var file in files) 89 { 90 Console.WriteLine(file); 91 } 92 Console.WriteLine("Get File Successfully..."); 93 94 } 95 }