图片

定义一个图片对象

  1. Image img = Image.FromFile(@"C:\Users\SpringRain\Desktop\Picture\1.jpg");

Directory文件夹

创建文件夹 支持递归创建

  1. //创建文件夹 支持递归创建
  2. Directory.CreateDirectory(@"G:\a\b");
  3. Console.WriteLine("创建成功");
  4. Console.ReadKey();

删除文件夹 支持递归删除

  1. Directory.Delete(@"G:\a", true);
  2. Console.WriteLine("删除成功");
  3. Console.ReadKey();

剪切文件夹

  1. Directory.Move(@"c:\a", @"C:\Users\SpringRain\Desktop\new");
  2. Console.WriteLine("剪切成功");
  3. Console.ReadKey();

获得指定文件夹下所有文件的全路径

  1. string[] path = Directory.GetFiles(@"C:\Users\SpringRain\Desktop\Picture","*.jpg");
  2. for (int i = 0; i < path.Length; i++)
  3. {
  4. Console.WriteLine(path[i]);
  5. }
  6. Console.ReadKey();

获得指定目录下所有文件夹的全路径

  1. string[] path = Directory.GetDirectories(@"C:\Users\SpringRain\Desktop\new");
  2. for (int i = 0; i < path.Length; i++)
  3. {
  4. Console.WriteLine(path[i]);
  5. }
  6. Console.ReadKey();

判断指定的文件夹是否存在

  1. if (Directory.Exists(@"C:\a\b"))
  2. {
  3. for (int i = 0; i < 100; i++)
  4. {
  5. Directory.CreateDirectory(@"C:\a\b\" + i);
  6. }
  7. }
  8. Console.WriteLine("OK");
  9. Console.ReadKey();

Path路径 (一直都是操作的字符串!!!)

通过一个文件的全路径得到这个文件的名字

  1. string filePath = @"C:\a\b\1.txt";
  2. string fileName = Path.GetFileName(filePath);
  3. //

通过一个文件的全路径得到这个文件的父路径

  1. string path = @"C:\Users\SpringRain\Desktop\0505.Net基础班第二十一天.txt";
  2. Console.WriteLine(Path.GetDirectoryName(path));
  3. //C:\Users\SpringRain\Desktop\

改变一个文件的后缀

  1. string path = @"C:\Users\SpringRain\Desktop\0505.Net基础班第二十一天.txt";
  2. Console.WriteLine(Path.ChangeExtension(path, "jpg"));
  3. //C:\Users\SpringRain\Desktop\0505.Net基础班第二十一天.jpg

File文件

写入文件

使用using,定义范围,在该范围结束时回收资源。
  注意使用前提:该对象必须继承了IDisposable接口,功能等同于try{}Finally{}。

  1. string str = "LittleBai";
  2. //创建写入字符串
  3. Byte[] bytesToWrite = Encoding.Default.GetBytes(str); ;
  4. //创建文件
  5. using (FileStream fs = new FileStream("test.txt", FileMode.Create))
  6. {
  7. //将字符串写入文件
  8. fs.Write(bytesToWrite, 0, bytesToWrite.Length);
  9. }

读取文件

  1. using (FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
  2. {
  3. byte[] buffer = new byte[1024 * 1024 * 5];
  4. //实际读取到的字节数
  5. int r = fsRead.Read(buffer, 0, buffer.Length);
  6. textBox1.Text = Encoding.Default.GetString(buffer, 0, r);
  7. }

判断文件是否存在

  1. File.Exists("Books.xml");

复制、剪切、创建、移除、读取数据、写入数据

  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 _02File
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. //操作文件的
  14. //复制、剪切、创建、移除
  15. //File.Create(@"C:\Users\SpringRain\Desktop\new.txt");
  16. //Console.WriteLine("创建成功");
  17. //File.Delete(@"C:\Users\SpringRain\Desktop\new.txt");
  18. //Console.WriteLine("删除成功");
  19. //File.Move(@"C:\Users\SpringRain\Desktop\0505.Net基础班第二十一天.txt", @"C:\Users\SpringRain\Desktop\1.txt");
  20. //Console.ReadKey();
  21. //使用File类来读取数据
  22. //byte[] buffer = File.ReadAllBytes(@"C:\Users\SpringRain\Desktop\1.txt");
  23. //string str = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
  24. //Console.WriteLine(str);
  25. ////编码:把字符串以怎样形式存储为二进制 ASCII GBK GB2312 UTF-8
  26. //Console.ReadKey();
  27. //string[] str = File.ReadAllLines(@"C:\Users\SpringRain\Desktop\1.txt",Encoding.Default);
  28. //for (int i = 0; i < str.Length; i++)
  29. //{
  30. // Console.WriteLine(str[i]);
  31. //}
  32. //string str = File.ReadAllText(@"C:\Users\SpringRain\Desktop\1.txt",Encoding.Default);
  33. //Console.WriteLine(str);
  34. //Console.ReadKey();
  35. //===============================================File类写入
  36. //string str="哈哈";
  37. //byte[] buffer=Encoding.Default.GetBytes(str);
  38. //File.WriteAllBytes(@"C:\Users\SpringRain\Desktop\new.txt", buffer);
  39. //Console.WriteLine("OK");
  40. //File.WriteAllLines(@"C:\Users\SpringRain\Desktop\1.txt", new string[] { "张三", "李四", "王五", "赵六" });
  41. //Console.WriteLine("OK");
  42. // File.WriteAllText(@"C:\Users\SpringRain\Desktop\1.txt", "今天还是比较凉快的");
  43. //File.AppendAllText(@"C:\Users\SpringRain\Desktop\1.txt", "没有覆盖哟");
  44. //Console.WriteLine("OK");
  45. //Console.ReadKey();
  46. }
  47. }
  48. }