当前位置:   article > 正文

C#文件操作_c# 创建文件夹

c# 创建文件夹

目录

一、文件夹创建

二、Path 路径配置

三、文件读写

四、磁盘操作

五、文档序列化


一、文件夹创建

常用函数:

Directory.Exists(path)                       -- 此文件夹是否存在
Directory.CreateDirectory(path)     -- 创建文件夹
Directory.Move(path_a, path_b)     -- 文件夹剪切
Directory.Delete(path)                       -- 只可删除空文件夹,若删除非空文件夹,会报异常
Directory.Delete(path, true)              -- 删除文件夹,包括里面的文档 

例程: 

  1. internal class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. string path_A = @"C:\\Temp\\A";
  6. string path_B = @"C:\\Temp\\B";
  7. if (!Directory.Exists(path_A)) //若此文件夹不存在
  8. {
  9. Directory.CreateDirectory(path_A); //创建此文件夹
  10. Directory.Move(path_A, path_B); //A文件夹剪切成B
  11. Directory.Delete(path_B, true); //删除B文件夹
  12. }
  13. }
  14. }

二、Path 路径配置

 常用函数:

Path.GetPathRoot(path)               -- 获取根目录
Path.GetDirectoryName(path)     -- 获取文件路径名
Path.GetFileNameWithoutExtension(path) -- 获取文件名 不包含后缀
Path.GetExtension(path)               -- 获取文件后缀
Path.GetFileName(path)                 -- 获取文件名 包含后缀
Path.GetFullPath(path)                   -- 获取文件绝对路径
Path.ChangeExtension(path, "text") -- 修改文件后缀
Path.GetExtension(path)                -- 获取文件后缀
Path.Combine("C:\\搜狗浏览器\\", "aaaa.txt") -- 合并文件路径

Path.GetRandomFileName()           -- 获取随机文件名+后缀
Path.PathSeparator                           -- 分隔符
Path.VolumeSeparatorChar            -- 冒号
Path.AltDirectorySeparatorChar    -- 反斜杠

例程: 

  1. internal class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. string path = @"C:\搜狗浏览器\ldinst-41.exe.ttd";
  6. //获取根目录
  7. Console.WriteLine(Path.GetPathRoot(path)); //结果显示: C:\
  8. //获取文件路径名
  9. Console.WriteLine(Path.GetDirectoryName(path)); //结果显示: C:\搜狗浏览器
  10. //获取文件名 不包含后缀
  11. Console.WriteLine(Path.GetFileNameWithoutExtension(path));//结果显示: ldinst-41.exe
  12. //获取文件后缀
  13. Console.WriteLine(Path.GetExtension(path));//结果显示: .ttd
  14. //获取文件名 包含后缀
  15. Console.WriteLine( Path.GetFileName(path)); //结果显示: ldinst-41.exe.ttd
  16. //获取绝对路径
  17. Console.WriteLine(Path.GetFullPath(path));//结果显示: C:\搜狗浏览器\ldinst-41.exe.ttd
  18. //修改文件后缀
  19. Console.WriteLine(Path.ChangeExtension(path, "text"));//结果显示: C:\搜狗浏览器\ldinst-41.exe.text
  20. //合并路径
  21. Console.WriteLine(Path.Combine(Path.GetDirectoryName(path), "aaaa.txt"));//结果显示: C:\搜狗浏览器\aaaa.txt
  22. //获取随机文件名+后缀
  23. Console.WriteLine(Path.GetRandomFileName()); //结果显示: vjzle4pq.1v0
  24. Console.WriteLine(Path.PathSeparator); //分隔符
  25. Console.WriteLine(Path.VolumeSeparatorChar);//冒号
  26. Console.WriteLine(Path.AltDirectorySeparatorChar); //反斜杠
  27. }
  28. }

三、文件读写

 常用函数:

File.Create(path)               -- 文档创建并清空
File.Copy(path_1,path_2) -- 文档复制
File.Delete(path_2)           -- 文档删除
File.Move(path_1,path_2) -- 文档剪切
File.AppendText(path)     -- 文档内容追加
File.ReadAllLine(path)     -- 文档读取所有行
File.ReadAllText(path)     -- 读取整个文档

FileStream.Write(bytes,0,bytes.Length) -- 一个个写
FileStream.Flush()   -- 刷新

StreamWriter.WriteLine(text) -- 写一行
StreamWrite.BaseStream.Write(bytes,0,bytes.Length) -- 一个个写
StreamWrite.Flush()  -- 刷新

例程: 

  1. internal class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. string path = "C:\\Temp\\A.txt";
  6. using (FileStream fs = File.Create(path))//创建并清空原有数据
  7. {
  8. byte[] bytes = Encoding.UTF8.GetBytes("我最最最爱你,你不知道吗\r\n");
  9. fs.Write(bytes, 0, bytes.Length); //一个个写
  10. fs.Flush();
  11. }
  12. using (StreamWriter sw = File.AppendText(path)) //流写入 -- 追加
  13. {
  14. string text = "我是你.......";
  15. sw.WriteLine(text);
  16. byte[] bytes = Encoding.UTF8.GetBytes("eeeeeeeee\r\n");
  17. sw.BaseStream.Write(bytes, 0, bytes.Length); //一个个写
  18. sw.Flush();
  19. }
  20. //File.OpenRead(path);等价
  21. using (FileStream fs = new FileStream(path, FileMode.Open)) //分批读取
  22. {
  23. byte[] bytes = new byte[1 * 1024];
  24. int count = fs.Read(bytes, 0, bytes.Length);
  25. string date = Encoding.UTF8.GetString(bytes, 0, count);
  26. Console.WriteLine(date);
  27. }
  28. foreach (string item in File.ReadAllLines(path)) //读取所有行,遍历
  29. {
  30. Console.WriteLine(item);
  31. }
  32. Console.WriteLine(File.ReadAllText(path)); //读取整个文本
  33. string path_1 = "C:\\Temp\\A.txt";
  34. string path_2 = "C:\\Temp\\B.txt";
  35. File.Copy(path_1, path_2); //文档复制
  36. File.Delete(path_2); //文档删除
  37. File.Move(path_1, path_2); //文档剪切
  38. }
  39. }

四、磁盘操作

 常用函数:

DriveInfo.GetDrives() -- 获取当前所有磁盘
drive.IsReady              -- 磁盘是否可用
drive.DriveType          -- 获取磁盘类型
drive.Name                  -- 磁盘名称
drive.TotalSize            -- 磁盘总空间
drive.AvailableFreeSpace -- 磁盘剩余空间

例程: 

  1. internal class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. foreach (DriveInfo drive in DriveInfo.GetDrives())
  6. {
  7. if (drive.IsReady)
  8. {
  9. Console.WriteLine($"类型:{drive.DriveType}" +
  10. $"名称:{drive.Name}" +
  11. $"总空间:{drive.TotalSize / 1024 / 1024 / 1024}G " +
  12. $"剩余空间:{drive.AvailableFreeSpace / 1024 / 1024 / 1024} G");
  13. }
  14. else
  15. {
  16. Console.WriteLine($"类型:{drive.DriveType} is not ready");
  17. }
  18. }
  19. }
  20. }

五、文档序列化

 常用函数:

二进制序列化
    BinaryFormatter.Serialize(stream, text)  -- 序列化成二进制,写入文档
    BinaryFormatter.Deserialize(stream)      --反序列化读出文档
    
XML序列化
    XmlSerializer.Serialize(stream, text)  -- 序列化成XML格式,写入文档
    XmlSerializer.Deserialize(stream)      --反序列化读取
    
Json序列化
    JsonConvert.SerializeObject(text)         -- 序列化
    JsonConvert.DeserializeObject(result) -- 反序列化

例程: 

  1. internal class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. string path = @"C:\Temp\C.txt";
  6. string text = "凄凄切切QQ群www";
  7. //---------------二进制序列化------------------------
  8. using (Stream stream = File.OpenWrite(path))
  9. {
  10. BinaryFormatter bf = new BinaryFormatter();
  11. //序列化文档
  12. bf.Serialize(stream, text);
  13. }
  14. using (Stream stream = File.OpenRead(path))
  15. {
  16. BinaryFormatter bf = new BinaryFormatter();
  17. //反序列化读取
  18. Console.WriteLine(bf.Deserialize(stream));
  19. }
  20. //---------------XML序列化------------------------
  21. using (Stream stream = File.OpenWrite(path))
  22. {
  23. XmlSerializer serializer = new XmlSerializer(typeof(string));
  24. //序列化文档
  25. serializer.Serialize(stream, text);
  26. }
  27. using (Stream stream = File.OpenRead(path))
  28. {
  29. XmlSerializer serializer = new XmlSerializer(typeof(string));
  30. //反序列化读取
  31. Console.WriteLine(serializer.Deserialize(stream));
  32. }
  33. //---------------Json序列化------------------------
  34. string result = JsonConvert.SerializeObject(text); //序列化
  35. Console.WriteLine(result);
  36. Console.WriteLine(JsonConvert.DeserializeObject(result)); //反序列化
  37. }
  38. }

如有错误,烦请批评指正

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/367313?site
推荐阅读
相关标签
  

闽ICP备14008679号