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


本篇主要涉及:
1、先判断文件夹是否存在,再决定是否创建文件夹
2、先判断文件是否存在,再决定是否创建文件
3、创建文件之前计算文件夹总大小,如果大小超过规定,放弃创建文件

 

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

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

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("必须规定创建文件夹的名称");
            }
        }
    }
}
 

结果:
如果文件夹不存在,多了一个新的文件夹:
1

 

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

 

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

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

using System.IO;
 
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = @"F:Testsecond.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();
            }
        }
    }
}
 

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

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

 

注意:
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	hird.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;
        }
    }
}
 

结果:
5

原文地址:https://www.cnblogs.com/darrenji/p/3652062.html