Folders and files are common, how to create them?
Should we first determine whether it exists? Very, very basic knowledge points
using System; using ; using ; using ; using ; namespace { public class WenJianLei { const string main_Dir = @"D:/WenTest"; const string wenjianpath = @"D:\WenTest\"; //Create folders based on the full path of the folder public static void CreateDir(string subdir) { string path = main_Dir + "/" + subdir; if ((path)) { ("This folder already exists, no need to create it!"); } else { (path); (path+"Create successfully!"); } } //Create a folder based on the folder name public static void CreateNameDir(string name) { if(!=0) { CreateDir(name); } else { ("The folder name must be specified to create it!"); } } public static void CreateWenJian() { if (!(wenjianpath)) { ("File creation succeeded!"); TextWriter tw = new StreamWriter(wenjianpath); ("The first line added after creating the file~~"); (); } else { TextWriter tw = new StreamWriter(wenjianpath,true); ("The file already exists, add another line~~"); (); } } //File size calculation public static void CreateMoreSize() { long size = GetDirectoryLength(@"D:\WenTest"); if (!(wenjianpath)) { if (size <= 1) { TextWriter tw = new StreamWriter(wenjianpath); ("The first line added after creating the file~~"); (); } else { ("Cannot be created, it has exceeded the limited size~~"); } } else { TextWriter tw = new StreamWriter(wenjianpath, true); ("The file already exists, add another line~~"); (); } } public static long GetDirectoryLength(string path) { if (!(path)) { return 0; } long size = 0; //Transfer all files under the specified path DirectoryInfo di = new DirectoryInfo(path); foreach (FileInfo fi in ()) { size += ; } //Travel through all folders under the specified path DirectoryInfo[] dis = (); if ( > 0) { for (int i = 0; i < ; i++) { size += GetDirectoryLength(dis[i].FullName); } } return size; } } }
First determine whether the folder exists, and then decide whether to create the folder
If the folder does not exist, create a new folder.
If the folder already exists, the prompt already exists.
using ; namespace ConsoleApplication26 { class Program { //The root folder of file storage const string main_Dir = @"F:/Test"; static void Main(string[] args) { CreateNamedDir("User01"); (); } //Create folders based on the full path of the folder private static void CreateDir(string subdir) { string path = main_Dir + "/" + subdir; if ((path)) { ("This folder already exists, no need to create it!"); } else { (path); (path+ "Create successfully!"); } } //Create a folder based on the folder name private static void CreateNamedDir(string name) { if ( != 0) { CreateDir(name); } else { ("The name of the folder that is created must be specified"); } } } }
result:
If the folder does not exist, there is a new folder.
If the folder already exists, the prompt already exists.
First determine whether the file exists, and then decide whether to create the file
If the file does not exist, create the file and write a line of content in the file.
If the file exists, add a line to the current file.
using ; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { string path = @"F:\Test\"; if (!(path)) { //(path); TextWriter tw = new StreamWriter(path); ("The first line added after creating the file~~"); (); } else { TextWriter tw = new StreamWriter(path,true); ("The file already exists, add another line~~"); (); } } } }
result:
If the file does not exist, create the file and write a line of content in the file.
If the file exists, add a line to the current file.
Notice:
(path)Returns FileStream type
TextWriter tw = new StreamWriter(path) returns TextWriter type
The 2-line statement cannot exist at the same time, otherwise the error "It is being used by another process, so the process cannot access this file".
Calculate the total folder size before creating a file. If the size exceeds the specified file, abandon the creation of the file.
using System; using ; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { string path = @"F:\Test\"; //Calculate the size of the specified folder long size = GetDirectoryLength(@"F:\Test"); if (!(path)) { if (size <= 500) { TextWriter tw = new StreamWriter(path); ("The first line added after creating the file~~"); (); } else { ("Cannot be created, it has exceeded the limited size~~"); } } else { TextWriter tw = new StreamWriter(path, true); ("The file already exists, add another line~~"); (); } //(()); (); } //Recursively calculate folder size static long GetDirectoryLength(string path) { if (!(path)) { return 0; } long size = 0; //Transfer all files under the specified path DirectoryInfo di = new DirectoryInfo(path); foreach (FileInfo fi in ()) { size += ; } //Travel through all folders under the specified path DirectoryInfo[] dis = (); if ( > 0) { for (int i = 0; i < ; i++) { size += GetDirectoryLength(dis[i].FullName); } } return size; } } }
result:
The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the following links