(C#基礎)創建文件,文件夾


    文件夾,文件這是常見的,怎么創建?要不要先判斷是否存在?非常非常基礎的知識點。

  代碼

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;
        }
    }
}

調用代碼

 // wenjian.WenJianLei.CreateNameDir("mytest1");
            //wenjian.WenJianLei.CreateWenJian();
            wenjian.WenJianLei.CreateMoreSize();

來源:http://www.cnblogs.com/darrenji/p/3652062.html

 

http://www.cnblogs.com/hqbhonker/p/3494042.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM