Path类 File类


Path类专门用来操作路径

string str =@"C:\Users\杰殇\Pictures\Saved Pictures\杰殇.wav";

            Console.WriteLine(Path.GetFileName(str));//获得最后的文件名 杰殇.wav

            Console.WriteLine(Path.GetFileNameWithoutExtension(str));//获得文件名,但不包含扩展名 杰殇

            Console.WriteLine(Path.GetExtension(str));//获得扩展名 .wav

            Console.WriteLine(Path.GetDirectoryName(str));//获得文件所在的文件夹名称 C:\Users\杰殇\Pictures\Saved Pictures

            Console.WriteLine(Path.GetFullPath(str));//获得文件全路径C:\Users\杰殇\Pictures\Saved Pictures\杰殇.wav

            Console.WriteLine(Path.Combine(@"C:\Users", @"杰殇\Pictures"));//连接两个字符串作为路径

 File.Create(@"C:\Users\杰殇\Desktop\C#记录\new.txt");//创建一个文件

            Console.WriteLine("创建成功");

            File.Delete(@"C:\Users\杰殇\Desktop\C#记录\new.txt");

            Console.WriteLine("删除成功");

            File.Copy(@"C:\Users\杰殇\Desktop\C#记录\Path类.DOCX", @"C:\Users\杰殇\Desktop\C#记录\new.DOCX");//复制一个文件

1.Path类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace _14Path类
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             string str = @"C:\3000soft\Red Spider\Data\Message\老赵.wav";
15             //获得文件名
16             Console.WriteLine(Path.GetFileName(str));
17             //获得文件名但是不包含扩展名
18             Console.WriteLine(Path.GetFileNameWithoutExtension(str));
19             //获得文件的扩展名
20             Console.WriteLine(Path.GetExtension(str));
21             //获得文件所在的文件夹的名称
22             Console.WriteLine(Path.GetDirectoryName(str));
23             //获得文件所在的全路径
24             Console.WriteLine(Path.GetFullPath(str));
25             //连接两个字符串作为路径
26             Console.WriteLine(Path.Combine(@"c:\a\" , "b.txt"));
27 
28 
29             //int index = str.LastIndexOf("\\");
30             //str = str.Substring(index + 1);
31             //Console.WriteLine(str);
32             Console.ReadKey();
33         }
34     }
35 }

2.File类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace _15File类
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14 
15             //创建一个文件
16             //File.Create(@"C:\Users\SpringRain\Desktop\new.txt");
17             //Console.WriteLine("创建成功");
18             //Console.ReadKey();
19 
20             //删除一个文件
21             //File.Delete(@"C:\Users\SpringRain\Desktop\new.txt");
22             //Console.WriteLine("删除成功");
23             //Console.ReadKey();
24             //1024byte=1kb
25             //1024kb=1M
26             //1024M=1G
27             //1024G=1T
28             //1024T=1PT
29 
30             //复制一个文件
31             //File.Copy(@"C:\Users\SpringRain\Desktop\code.txt", @"C:\Users\SpringRain\Desktop\new.txt");
32             //Console.WriteLine("复制成功");
33             //Console.ReadKey();
34 
35 
36             //剪切
37             //File.Move(@"C:\Users\SpringRain\Desktop\code.txt", @"C:\Users\SpringRain\Desktop\newnew.txt");
38             //Console.WriteLine("剪切成功");
39             //Console.ReadKey();
40 
41             File.Create(@"d:\桌面\a.txt");
42             //Console.WriteLine(sizeof(char));
43             //Console.ReadKey();
44             //Console.WriteLine(sizeof(string));a  "dsfdsfds"
45             //Console.ReadKey();
46         }
47     }
48 }

3.使用File类操作文件的数据

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.IO;
 7 namespace _16_使用File类操作文件的数据
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //byte[] buffer = File.ReadAllBytes(@"C:\Users\SpringRain\Desktop\1.txt");
14 
15 
16             //EncodingInfo[] en = Encoding.GetEncodings();
17             //foreach (var item in en)
18             //{
19             //    Console.WriteLine(item.DisplayName);
20             //}
21             //Console.ReadKey();
22             //将字节数组转换成字符串
23             //string s = Encoding.UTF8.GetString(buffer);
24             //Console.WriteLine(s);
25             // Console.WriteLine(buffer.ToString());
26             //编码格式:指的就是你以怎样的形式来存储字符串
27             //a-z 0-9  Ascii  117 u---->汉字--->GB2312 GBK
28             //int n = (int)'u';
29             //char c = (char)188;
30             //Console.WriteLine(c);
31             ////Console.WriteLine(n);
32 
33 
34             //string s="今天天气好晴朗,处处好风光";
35             ////将字符串转换成字节数组
36             //byte[] buffer=  Encoding.Default.GetBytes(s);
37             ////以字节的形式向计算机中写入文本文件
38             //File.WriteAllBytes(@"C:\Users\SpringRain\Desktop\1.txt", buffer);
39             //Console.WriteLine("写入成功");
40             //Console.ReadKey();
41 
42 
43             //使用File类来实现一个多媒体文件的复制操作
44 
45             //读取
46             byte[] buffer = File.ReadAllBytes(@"C:\Users\SpringRain\Desktop\12333.wmv");
47             Console.ReadKey();
48 
49             ////写入
50             //File.WriteAllBytes(@"C:\Users\SpringRain\Desktop\new.wav", buffer);
51             //Console.WriteLine("复制成功");
52             //Console.ReadKey();
53 
54 
55             //byte[] buffer=new byte[1024*1024*5];
56             //while (true)
57             //{
58             //    File.WriteAllBytes(@"C:\Users\SpringRain\Desktop\123.wmv", buffer);
59             //}
60 
61         }
62     }
63 }