///// Create a folder /////public static void FolderCreate (String Path) {//To determine if the destination directory exists if it does not exist then create the IF (! Directory.Exists (path)) directory.createdirectory (path); #endregion #region Create directory public static void Filecreate (string path) {FileInfo CreateFile = new FileInfo (Path);//Create File if (! createfile.exists) {FileStream FS = Createfile.create (); Fs. Close (); }} #endregion #region recursively delete folder directory and file/**************************************** * Function name: DeleteFolder * Function Description: Recursively delete folder directory and file * reference Number: Dir: Folder path * call to Column: * String dir = Server.MapPath ("test/"); * EC. Fileobj.deletefolder (dir); *****************************************/ //////recursively Delete folder directories and files///////// public static void DeleteFolder (String dir) {if (directory.exists (dir))////If this folder is present, delete {foreach (string d in DIRECTORY.G Etfilesystementries (dir)) {if (file.exists (d)) File.delete (d);//delete files directly in the Else DeleteFolder (d);//recursively Delete subfolders} Directory . Delete (dir, true); Delete empty Folder}} #endregion #region Copy all the contents below the specified folder to the destination folder The target folder is read-only and will be error-free. /**************************************** * Function Name: copydir * Function Description: Copy all the contents below the specified folder to the destination folder The target folder is read-only and the error will be given. * Parameters: Srcpath: Original path, Aimpath: Target folder * Call shown column: * String srcpath = Server.MapPath ("test/"); * String aimpath = Server.MapPath ("test1/"); * EC. Fileobj.copydir (Srcpath,aimpath); *****************************************/ ////// Specify the contents of the folder below copy to the target folder /////Original path///Destination Folder public static void Copydir (String srcpath, String aimpath) {try {//Check whether the destination directory ends with a directory split character if it is not added if (aimpath[aimpath.lengt H-1]! = Path.directoryseparatorchar) Aimpath + = Path.directoryseparatorchar; Determine if the target directory exists if it does not exist then create the IF (! Directory.Exists (Aimpath)) directory.createdirectory (Aimpath); Get the list of files in the source directory, which is an array containing the files and directory paths//If you point to the file below the copy destination file without the directory, use the following method//string[] fileList = Directory.GetFiles ( Srcpath); string[] fileList = directory.getfilesystementries (Srcpath); Traverse all files and directories foreach (string file in FileList) {/////first as directory processing if this directory is present recursively copy the file under the directory if (directory.exists (file)) Copydir ( File, Aimpath + path.getfilename (file)); Otherwise direct Copy file else file.copy (file, Aimpath + path.getfilename (file), true); }} catch (Exception ee) {throw new Exception (EE. ToString ()); }} #endregion #region get all subdirectories and files under the specified folder (tree)/**************************************** * Function Name: Getfoldall (String Path) * Function Description: Gets all subdirectories and files under the specified folder (tree) * Parameters: Path: Verbose path * Call to Columns: * String strdirlist = Server.MapPath ("templates"); * this. LiTeral1. Text = EC. Fileobj.getfoldall (strdirlist); *****************************************/ //////Get all subdirectories and files under the specified folder/////Detailed pathpublic static string Getfoldall (String Path) {string str = ""; DirectoryInfo Thisone = new DirectoryInfo (Path); str = listtreeshow (thisone, 0, str); return str; } //////Get all subdirectories and file functions under the specified folder/////Specify directory///Default start value, when called, is typically 0///The incoming value used for the overlay, which is generally empty/// public static string Listtreeshow (DirectoryInfo thedir, int nlevel, string Rn)//recursive directory file {directoryinfo[] subdirectories = Thedir.getdirectories ();//Get directory foreach (DirectoryInfo dirinfo in subdirectories) {if (Nlevel = = 0) {Rn + = "├";} else { String _s = ""; for (int i = 1; I <= nlevel; i++) {_s + = "│";} Rn + = _s + "├"; } Rn + = "
"+ dirinfo. Name.tostring () + "
"; fileinfo[] FileInfo = Dirinfo. GetFiles (); File in the directory foreach (FileInfo finfo in FileInfo) {if (Nlevel = = 0) {Rn + = "│";} else {string _f = ""; for (int i = 1; I <= nlevel; i++) {_f + = "│";} Rn + = _f + "│"; } Rn + = fInfo.Name.ToString () + "
"; } RN = Listtreeshow (dirinfo, Nlevel + 1, RN); } return Rn; }/**************************************** * Function name: Getfoldall (string Path) * Function Description: Gets all subdirectories and files under the specified folder (drop-down box) * Parameter: Path: Detailed path * Calls to the following columns: * String strdirlist = Server.MapPath ("templates"); * this. Literal2.text = EC. Fileobj.getfoldall (Strdirlist, "TPL", ""); *****************************************/ ////// get all subdirectories and files under the specified folder (dropdown box) /////Detailed path///Drop-down list name///Default selection template Namepublic static string Getfoldall (String path,string dropname,string tplpath) {string strdrop = "--Please select the detailed template--"; String str = ""; DirectoryInfo Thisone = new DirectoryInfo (Path); str = listtreeshow (thisone, 0, Str,tplpath); Return strdrop+str+ ""; } //////Get all subdirectories and file functions under the specified folder/////Specify directory///Default start value, when called, is typically 0///The incoming value used for the overlay, which is generally empty///Default selection template Name/// public static string Listtreeshow (DirectoryInfo thedir, int nlevel, string rn,string tplpath)//recursive directory file {directoryinfo[] s Ubdirectories = Thedir.getdirectories ();//Get directory foreach (DirectoryInfo dirinfo in subdirectories) {Rn + = "<="nLevel;" i++)="" "┣";=""> < option="">"; fileinfo[] FileInfo = Dirinfo. GetFiles (); File in directory foreach (FileInfo finfo in FileInfo) {Rn + = ""; } RN = Listtreeshow (dirinfo, Nlevel + 1, RN, Tplpath); } return Rn; #endregion #region Get the folder size/**************************************** * Function Name: getdirectorylength (String dirpath) * Function Description: Get folder Size * Parameter: Dirpath: folder verbose path * Call Example column: * String path = Server.MapPath ("templates"); * Response.Write (EC. Fileobj.getdirectorylength (Path)); *****************************************/ ////// Get folder size /////Folder path/// public static long Getdirectorylength (string dirpath) {if (! Directory.Exists (Dirpath)) return 0; Long len = 0; DirectoryInfo di = new DirectoryInfo (Dirpath); foreach (FileInfo fi in Di. GetFiles ()) {len + = fi. Length; } directoryinfo[] dis = di. GetDirectories (); if (DIS. Length > 0) {for (int i = 0; i < dis. Length; i++) {len + getdirectorylength (dis[i). FullName); }} return len; #endregion #region Get the specified file detail property/**************************************** * Function Name: Getfileattibe (String filePath) * Function Description: Get the specified file detail properties * Parameters: FilePath: File verbose path * Call column: * string file = Server.MapPath ("robots.txt"); * Response.Write (EC. Fileobj.getfileattibe (file)); *****************************************/ ////// get the specified file detail attributes /////File detail Path/// public static string Getfileattibe (String filePath) {string str = ""; System.IO.FileInfo Objfi = new System.IO.FileInfo (FilePath); str + = "Verbose path:" + Objfi.fullname + "
File name: "+ Objfi.name +"
File length: "+ objFI.Length.ToString () +" bytes
Create Time "+ objFI.CreationTime.ToString () +"
Last access time: "+ objFI.LastAccessTime.ToString () +"
Modification time: "+ objFI.LastWriteTime.ToString () +"
Directory: "+ Objfi.directoryname +"
Extension: "+ objfi.extension; return str; } #endregion}}
ASP. Net file and folder operations encyclopedia