C#先判断是否存在再创建文件夹或文件与递归计算文件夹大小

Tia ·
更新时间:2024-09-20
· 866 次阅读

文件夹,文件这是常见的,怎么创建?

要不要先判断是否存在?非常非常基础的知识点

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace dazilianxi.wenjian { public class WenJianLei { const string main_Dir = @"D:/WenTest"; const string wenjianpath = @"D:\WenTest\second.txt"; //根据文件夹全路径创建文件夹 public static void CreateDir(string subdir) { string path = main_Dir + "/" + subdir; if (Directory.Exists(path)) { Console.WriteLine("此文件夹已经存在,无需创建!"); } else { Directory.CreateDirectory(path); Console.WriteLine(path+" 创建成功!"); } } //根据文件夹名称创建文件夹 public static void CreateNameDir(string name) { if(name.Length!=0) { CreateDir(name); } else { Console.WriteLine("必须指定文件夹名称,才能创建!"); } } public static void CreateWenJian() { if (!File.Exists(wenjianpath)) { Console.WriteLine("文件创建成功!"); TextWriter tw = new StreamWriter(wenjianpath); tw.WriteLine("创建完文件加的第一行~~"); tw.Close(); } else { TextWriter tw = new StreamWriter(wenjianpath,true); tw.WriteLine("已经存在文件,再加一行吧~~"); tw.Close(); } } //文件大小计算 public static void CreateMoreSize() { long size = GetDirectoryLength(@"D:\WenTest"); if (!File.Exists(wenjianpath)) { if (size <= 1) { TextWriter tw = new StreamWriter(wenjianpath); tw.WriteLine("创建完文件加的第一行~~"); tw.Close(); } else { Console.WriteLine("无法创建,已经超过限定大小了~~"); } } else { TextWriter tw = new StreamWriter(wenjianpath, true); tw.WriteLine("已经存在文件,再加一行吧~~"); tw.Close(); } } public static long GetDirectoryLength(string path) { if (!Directory.Exists(path)) { return 0; } long size = 0; //遍历指定路径下的所有文件 DirectoryInfo di = new DirectoryInfo(path); foreach (FileInfo fi in di.GetFiles()) { size += fi.Length; } //遍历指定路径下的所有文件夹 DirectoryInfo[] dis = di.GetDirectories(); if (dis.Length > 0) { for (int i = 0; i < dis.Length; i++) { size += GetDirectoryLength(dis[i].FullName); } } return size; } } } 先判断文件夹是否存在,再决定是否创建文件夹

如果文件夹不存在,就创建新文件夹。
如果文件夹已存在,提示已经存在。

using System.IO; namespace ConsoleApplication26 { class Program { //文件存放的根文件夹 const string main_Dir = @"F:/Test"; static void Main(string[] args) { CreateNamedDir("User01"); Console.ReadKey(); } //根据文件夹全路径创建文件夹 private static void CreateDir(string subdir) { string path = main_Dir + "/" + subdir; if (Directory.Exists(path)) { Console.WriteLine("此文件夹已经存在,无需创建!"); } else { Directory.CreateDirectory(path); Console.WriteLine(path+ "创建成功!"); } } //根据文件夹名称创建文件夹 private static void CreateNamedDir(string name) { if (name.Length != 0) { CreateDir(name); } else { Console.WriteLine("必须规定创建文件夹的名称"); } } } }

结果:

如果文件夹不存在,多了一个新的文件夹。

如果文件夹已存在,提示已经存在。

先判断文件是否存在,再决定是否创建文件

如果文件不存在,创建文件并在文件中写入一行内容。
如果文件存在,在当前文件中追加一行内容。

using System.IO; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { string path = @"F:\Test\second.txt"; if (!File.Exists(path)) { //File.Create(path); TextWriter tw = new StreamWriter(path); tw.WriteLine("创建完文件加的第一行~~"); tw.Close(); } else { TextWriter tw = new StreamWriter(path,true); tw.WriteLine("已经存在文件,再加一行吧~~"); tw.Close(); } } } }

结果:

如果文件不存在,创建文件并在文件中写入一行内容。

如果文件存在,在当前文件中追加一行内容。

注意:

File.Create(path)返回FileStream类型
TextWriter tw = new StreamWriter(path)返回TextWriter类型
2行语句不能同时存在,否则报错"正由另一进程使用,因此该进程无法访问此文件"。

创建文件之前计算文件夹总大小,如果大小超过规定,放弃创建文件 using System; using System.IO; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { string path = @"F:\Test\third.txt"; //计算指定文件夹的大小 long size = GetDirectoryLength(@"F:\Test"); if (!File.Exists(path)) { if (size <= 500) { TextWriter tw = new StreamWriter(path); tw.WriteLine("创建完文件加的第一行~~"); tw.Close(); } else { Console.WriteLine("无法创建,已经超过限定大小了~~"); } } else { TextWriter tw = new StreamWriter(path, true); tw.WriteLine("已经存在文件,再加一行吧~~"); tw.Close(); } //Console.WriteLine(size.ToString()); Console.ReadKey(); } //递归计算文件夹大小 static long GetDirectoryLength(string path) { if (!Directory.Exists(path)) { return 0; } long size = 0; //遍历指定路径下的所有文件 DirectoryInfo di = new DirectoryInfo(path); foreach (FileInfo fi in di.GetFiles()) { size += fi.Length; } //遍历指定路径下的所有文件夹 DirectoryInfo[] dis = di.GetDirectories(); if (dis.Length > 0) { for (int i = 0; i < dis.Length; i++) { size += GetDirectoryLength(dis[i].FullName); } } return size; } } }

结果:

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对软件开发网的支持。如果你想了解更多相关内容请查看下面相关链接



C# 递归

需要 登录 后方可回复, 如果你还没有账号请 注册新账号