Path类,File类,File类练习.

Path类学习:
1
static void Main(string[] args) 2 { 3 string str = @"C:\3000soft\Red Spider\Data\Message\老赵.wav"; 4 //获得文件名 5 Console.WriteLine(Path.GetFileName(str)); 6 //获得文件名但是不包含扩展名 7 Console.WriteLine(Path.GetFileNameWithoutExtension(str)); 8 //获得文件的扩展名 9 Console.WriteLine(Path.GetExtension(str)); 10 //获得文件所在的文件夹的名称 11 Console.WriteLine(Path.GetDirectoryName(str)); 12 //获得文件所在的全路径 13 Console.WriteLine(Path.GetFullPath(str)); 14 //连接两个字符串作为路径 15 Console.WriteLine(Path.Combine(@"c:\a\" , "b.txt")); 16 17 int index = str.LastIndexOf("\\"); 18 str = str.Substring(index + 1); 19 Console.WriteLine(str); 20 Console.ReadKey(); 21 }
File类学习:
1
class Program 2 { 3 static void Main(string[] args) 4 { 5 //创建一个文件 6 File.Create(@"C:\Users\SpringRain\Desktop\new.txt"); 7 Console.WriteLine("创建成功"); 8 Console.ReadKey(); 9 10 //删除一个文件 11 File.Delete(@"C:\Users\SpringRain\Desktop\new.txt"); 12 Console.WriteLine("删除成功"); 13 Console.ReadKey(); 14 15 //复制一个文件 16 File.Copy(@"C:\Users\SpringRain\Desktop\code.txt", @"C:\Users\SpringRain\Desktop\new.txt"); 17 Console.WriteLine("复制成功"); 18 Console.ReadKey(); 19 20 //剪切 21 File.Move(@"C:\Users\SpringRain\Desktop\code.txt", @"C:\Users\SpringRain\Desktop\newnew.txt"); 22 Console.WriteLine("剪切成功"); 23 Console.ReadKey(); 24 } 25 }
练习:
1
class Program 2 { 3 static void Main(string[] args) 4 { 5 //EncodingInfo[] en = Encoding.GetEncodings(); 6 //foreach (var item in en) 7 //{ 8 // Console.WriteLine(item.DisplayName); 9 //} 10 //Console.ReadKey(); 11 12 /*将字节数组转换成字符串*/ 13 //byte[] buffer = File.ReadAllBytes(@"C:\Users\SpringRain\Desktop\1.txt"); 14 //string s = Encoding.UTF8.GetString(buffer); 15 //Console.WriteLine(s); 16 //Console.WriteLine(buffer.ToString()); 17 18 /*编码格式:指的就是你以怎样的形式来存储字符串*/ 19 //a-z 0-9 Ascii 117 u---->汉字--->GB2312 GBK 20 //int n = (int)'u'; 21 //char c = (char)188; 22 //Console.WriteLine(c); 23 ////Console.WriteLine(n); 24 25 26 //string s="今天天气好晴朗,处处好风光"; 27 /*将字符串转换成字节数组*/ 28 //byte[] buffer= Encoding.Default.GetBytes(s); 29 /*以字节的形式向计算机中写入文本文件*/ 30 //File.WriteAllBytes(@"C:\Users\SpringRain\Desktop\1.txt", buffer); 31 //Console.WriteLine("写入成功"); 32 //Console.ReadKey(); 33 34 35 /*使用File类来实现一个多媒体文件的复制操作*/ 36 //读取 37 //byte[] buffer = File.ReadAllBytes(@"C:\Users\SpringRain\Desktop\12333.wmv"); 38 //Console.ReadKey(); 39 ////写入 40 //File.WriteAllBytes(@"C:\Users\SpringRain\Desktop\new.wav", buffer); 41 //Console.WriteLine("复制成功"); 42 //Console.ReadKey(); 43 44 45 //byte[] buffer=new byte[1024*1024*5]; 46 //while (true) 47 //{ 48 // File.WriteAllBytes(@"C:\Users\SpringRain\Desktop\123.wmv", buffer); 49 //} 50 } 51 }

 

posted @ 2020-09-22 09:32  技术不够脸来凑  阅读(166)  评论(0)    收藏  举报