C#先判斷是否存在再創(chuàng)建文件夾或文件與遞歸計(jì)算文件夾大小
文件夾,文件這是常見(jiàn)的,怎么創(chuàng)建?
要不要先判斷是否存在?非常非?;A(chǔ)的知識(shí)點(diǎn)
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"; //根據(jù)文件夾全路徑創(chuàng)建文件夾 public static void CreateDir(string subdir) { string path = main_Dir + "/" + subdir; if (Directory.Exists(path)) { Console.WriteLine("此文件夾已經(jīng)存在,無(wú)需創(chuàng)建!"); } else { Directory.CreateDirectory(path); Console.WriteLine(path+" 創(chuàng)建成功!"); } } //根據(jù)文件夾名稱創(chuàng)建文件夾 public static void CreateNameDir(string name) { if(name.Length!=0) { CreateDir(name); } else { Console.WriteLine("必須指定文件夾名稱,才能創(chuàng)建!"); } } public static void CreateWenJian() { if (!File.Exists(wenjianpath)) { Console.WriteLine("文件創(chuàng)建成功!"); TextWriter tw = new StreamWriter(wenjianpath); tw.WriteLine("創(chuàng)建完文件加的第一行~~"); tw.Close(); } else { TextWriter tw = new StreamWriter(wenjianpath,true); tw.WriteLine("已經(jīng)存在文件,再加一行吧~~"); tw.Close(); } } //文件大小計(jì)算 public static void CreateMoreSize() { long size = GetDirectoryLength(@"D:\WenTest"); if (!File.Exists(wenjianpath)) { if (size <= 1) { TextWriter tw = new StreamWriter(wenjianpath); tw.WriteLine("創(chuàng)建完文件加的第一行~~"); tw.Close(); } else { Console.WriteLine("無(wú)法創(chuàng)建,已經(jīng)超過(guò)限定大小了~~"); } } else { TextWriter tw = new StreamWriter(wenjianpath, true); tw.WriteLine("已經(jīng)存在文件,再加一行吧~~"); 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; } } }
先判斷文件夾是否存在,再?zèng)Q定是否創(chuàng)建文件夾
如果文件夾不存在,就創(chuàng)建新文件夾。
如果文件夾已存在,提示已經(jīng)存在。
using System.IO; namespace ConsoleApplication26 { class Program { //文件存放的根文件夾 const string main_Dir = @"F:/Test"; static void Main(string[] args) { CreateNamedDir("User01"); Console.ReadKey(); } //根據(jù)文件夾全路徑創(chuàng)建文件夾 private static void CreateDir(string subdir) { string path = main_Dir + "/" + subdir; if (Directory.Exists(path)) { Console.WriteLine("此文件夾已經(jīng)存在,無(wú)需創(chuàng)建!"); } else { Directory.CreateDirectory(path); Console.WriteLine(path+ "創(chuàng)建成功!"); } } //根據(jù)文件夾名稱創(chuàng)建文件夾 private static void CreateNamedDir(string name) { if (name.Length != 0) { CreateDir(name); } else { Console.WriteLine("必須規(guī)定創(chuàng)建文件夾的名稱"); } } } }
結(jié)果:
如果文件夾不存在,多了一個(gè)新的文件夾。
如果文件夾已存在,提示已經(jīng)存在。
先判斷文件是否存在,再?zèng)Q定是否創(chuàng)建文件
如果文件不存在,創(chuàng)建文件并在文件中寫(xiě)入一行內(nèi)容。
如果文件存在,在當(dāng)前文件中追加一行內(nèi)容。
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("創(chuàng)建完文件加的第一行~~"); tw.Close(); } else { TextWriter tw = new StreamWriter(path,true); tw.WriteLine("已經(jīng)存在文件,再加一行吧~~"); tw.Close(); } } } }
結(jié)果:
如果文件不存在,創(chuàng)建文件并在文件中寫(xiě)入一行內(nèi)容。
如果文件存在,在當(dāng)前文件中追加一行內(nèi)容。
注意:
File.Create(path)返回FileStream類型
TextWriter tw = new StreamWriter(path)返回TextWriter類型
2行語(yǔ)句不能同時(shí)存在,否則報(bào)錯(cuò)"正由另一進(jìn)程使用,因此該進(jìn)程無(wú)法訪問(wèn)此文件"。
創(chuàng)建文件之前計(jì)算文件夾總大小,如果大小超過(guò)規(guī)定,放棄創(chuàng)建文件
using System; using System.IO; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { string path = @"F:\Test\third.txt"; //計(jì)算指定文件夾的大小 long size = GetDirectoryLength(@"F:\Test"); if (!File.Exists(path)) { if (size <= 500) { TextWriter tw = new StreamWriter(path); tw.WriteLine("創(chuàng)建完文件加的第一行~~"); tw.Close(); } else { Console.WriteLine("無(wú)法創(chuàng)建,已經(jīng)超過(guò)限定大小了~~"); } } else { TextWriter tw = new StreamWriter(path, true); tw.WriteLine("已經(jīng)存在文件,再加一行吧~~"); tw.Close(); } //Console.WriteLine(size.ToString()); Console.ReadKey(); } //遞歸計(jì)算文件夾大小 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; } } }
結(jié)果:
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
C#實(shí)現(xiàn)Windows服務(wù)安裝卸載開(kāi)啟停止
本文主要介紹了C#實(shí)現(xiàn)Windows服務(wù)安裝卸載開(kāi)啟停止,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07C# InitializeComponent()方法案例詳解
這篇文章主要介紹了C# InitializeComponent()方法案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08c#求點(diǎn)到直線的投影點(diǎn)坐標(biāo)
這篇文章主要介紹了c#求直線外一點(diǎn)到該直線的投影點(diǎn),大家參考使用吧2013-12-12C# TextBox 擴(kuò)展方法數(shù)據(jù)驗(yàn)證詳細(xì)說(shuō)明
C# TextBox 擴(kuò)展方法數(shù)據(jù)驗(yàn)證詳細(xì)說(shuō)明,需要的朋友可以參考一下2013-03-03C#實(shí)現(xiàn)帶行數(shù)和標(biāo)尺的RichTextBox
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)帶行數(shù)和標(biāo)尺的RichTextBox,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12C#中Task.ContinueWith連續(xù)任務(wù)使用實(shí)例
本文主要介紹了C#中Task.ContinueWith連續(xù)任務(wù)使用實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02