Asp.net file, folder

Source: Internet
Author: User

Using system; <br/> using system. data; <br/> using system. configuration; <br/> using system. collections; <br/> using system. web; <br/> using system. web. security; <br/> using system. web. ui; <br/> using system. web. UI. webcontrols; <br/> using system. web. UI. webcontrols. webparts; <br/> using system. web. UI. htmlcontrols; <br/> using system. io; <br/> Public partial class default2: system. web. UI. page <br/> {<br/> pro Tected void page_load (Object sender, eventargs E) <br/>{< br/>}< br/> # region create a folder <br/> /// <summary> <br/> // create a folder <br/> /// </Summary> <br/> /// <Param name = "path"> </param> <br/> Public static void foldercreate (string path) <br/>{< br/> // determine whether the target directory exists. If it does not exist, create it. <br/> If (! Directory. exists (PATH) <br/> directory. createdirectory (PATH); <br/>}< br/> # endregion <br/> # region create directory <br/> Public static void filecreate (string path) <br/>{< br/> fileinfo createfile = new fileinfo (PATH); // create a file <br/> If (! Createfile. exists) <br/>{< br/> filestream FS = createfile. create (); <br/> FS. close (); <br/>}< br/> # endregion <br/> # region recursively deletes folder directories and files <br/> /******* * ******************************* <br/> * function name: deletefolder <br/> * Function Description: recursively Delete folder directories and files <br/> * parameter: Dir: folder path <br/> * Call display column: <br/> * string dir = server. mappath ("test/"); <br/> * EC. fileobj. deletefolder (DIR); <br/> *************************** * *************/<Br/> // <summary> <br/> // recursively Delete folder directories and files <br />/// </Summary> <br/> /// <Param name = "dir"> </param> <br/> /// <returns> </ returns> <br/> Public static void deletefolder (string DIR) <br/>{< br/> If (directory. exists (DIR) // Delete the folder <br/>{< br/> foreach (string D in directory. getfilesystementries (DIR) <br/>{< br/> If (file. exists (D) <br/> file. delete (d); // delete the files directly. <br/> E LSE <br/> deletefolder (d); // recursively Delete subfolders <br/>}< br/> directory. delete (Dir, true ); // delete an empty folder <br/>}< br/> # endregion <br/> # region copies all the content in the specified folder to the target folder. if the target folder is read-only, an error is returned. <Br/> /*********************************** * ***** <br/> * Function Name: copydir <br/> * Function Description: Copy all the content in the specified folder to the target folder. If the target folder is read-only, an error is returned. <Br/> * parameter: srcpath: original path, aimpath: Target folder <br/> * Call display column: <br/> * string srcpath = server. mappath ("test/"); <br/> * string aimpath = server. mappath ("test1/"); <br/> * EC. fileobj. copydir (srcpath, aimpath ); <br/> ************************************ * ***/<br/> /// <summary> <br/> // copy all the content in the specified folder to the target folder. <br/> // /</Summary> <br/> // <Param name = "srcpath"> original path </param> <br/> // <Param name = "aimpath"> Target folder </param> <br/> Public static void copydir (string srcpath, string aimpath) <br/>{< br/> try <br/> {<br/> // check whether the target directory ends with a directory delimiter. If not, add it. <br/> If (aimpath [aimpath. length-1]! = Path. directoryseparatorchar) <br/> aimpath + = path. directoryseparatorchar; <br/> // you can create a directory if the target directory does not exist. <br/> If (! Directory. exists (aimpath) <br/> directory. createdirectory (aimpath); <br/> // obtain the file list of the source directory, this is an array containing files and directory paths <br/> // use the following method if you point to the file under the copy target file and do not include a directory <br/>/ /string [] filelist = directory. getfiles (srcpath); <br/> string [] filelist = directory. getfilesystementries (srcpath); <br/> // traverse all files and directories <br/> foreach (string file in filelist) <br/> {<br/> // process the data as a directory first. If the directory exists, recursively copy the files in the directory. <br/> If (directory. E Xists (File) <br/> copydir (file, aimpath + path. getfilename (File); <br/> // otherwise, copy the file directly <br/> else <br/> file. copy (file, aimpath + path. getfilename (file), true); <br/>}< br/> catch (exception ee) <br/>{< br/> throw new exception (EE. tostring (); <br/>}< br/> # endregion <br/> # region obtains all subdirectories and files in the specified folder (tree-like) <br/> /*********************************** * ***** <br/> * Function Name: getfoldall (string Path) <br/> * Function Description: obtain all subdirectories and files in the specified folder (tree) <br/> * parameter: Path: detailed path <br/> * Call display column: <br/> * string strdirlist = server. mappath ("templates"); <br/> * This. literal1.text = EC. fileobj. getfoldall (strdirlist ); <br/> ************************************ *****/<br/> /// <summary> <br/> // obtain all subdirectories and files in the specified folder <br/> /// </ summary> <br/> // <Param name = "path"> detailed path </param> <br/> Public static string getfoldall (strin G path) <br/>{< br/> string STR = ""; <br/> directoryinfo thisone = new directoryinfo (PATH ); <br/> STR = listtreeshow (thisone, 0, STR); <br/> return STR; <br/>}< br/> /// <summary> <br/> // obtain all subdirectories and file functions in the specified folder <br/> /// </ summary> <br/> /// <Param name = "thedir"> specify the directory </param> <br/> /// <Param name = "nlevel"> default start point value, generally, the value is 0 </param> <br/> // <Param name = "RN"> used for superposition, generally blank </param> <br/> /// <returns> </Returns> <br/> Public static string listtreeshow (directoryinfo thedir, int nlevel, string RN) // recursive directory file <br/>{< br/> directoryinfo [] subdirectories = thedir. getdirectories (); // get the directory <br/> foreach (directoryinfo dirinfo in subdirectories) <br/>{< br/> If (nlevel = 0) <br/> {<br/> rn + = "Beijing "; <br/>}< br/> else <br/> {<br/> string _ S = ""; <br/> for (INT I = 1; I <= nlevel; I ++) <br/>{< br/> _ s + = "│"; <Br/>}< br/> rn + = _ S + "yellow "; <br/>}< br/> rn + = "<B>" + dirinfo. name. tostring () + "</B> <br/>"; <br/> fileinfo [] fileinfo = dirinfo. getfiles (); // file in the directory <br/> foreach (fileinfo finfo in fileinfo) <br/>{< br/> If (nlevel = 0) <br/>{< br/> rn + = "│ ├ "; <br/>}< br/> else <br/> {<br/> string _ f = ""; <br/> for (INT I = 1; I <= nlevel; I ++) <br/>{< br/> _ F + = "│"; <br/>}< br/> rn + = _ F + "│ ├"; <br/>}< br/> rn + = finfo. name. tostring () + "<br/>"; <br/>}< br/> Rn = listtreeshow (dirinfo, nlevel + 1, RN ); </P> <p >}< br/> return rn; <br/>}</P> <p> /*************************** * ************ <br/> * Function Name: getfoldall (string path) <br/> * Function Description: obtains all subdirectories and files in a specified folder (in the drop-down box). <br/> * parameter: Path: detailed path <br/> * Call display column: <br/> * string strdirlist = server. mappath ("templates"); <br/> * This. literal2.text = EC. fileobj. getfoldall (strdirlist, "TPL ",""); <br/> ************************************ * ***/<br/> /// <summary> <br/> // obtain all subdirectories and files in the specified folder (in the drop-down box) <br/> // </Summary> <br/> // <Param name = "path"> detailed path </param> <br/> // <param name = "dropname"> drop-down list name </param> <br/> // <Param name = "tplpath"> default Template Name </param> <br/> Public static string getfoldall (string path, string dropname, string tplpath) <br/> {<br /> String strdrop = "<select name =/" "+ dropname +"/"id =/" "+ dropname +"/"> <option value =/"/"> -- select a detailed template -- </option> "; <br/> string STR = ""; <br/> directoryinfo thisone = new directoryinfo (PATH); <br/> STR = listtreeshow (thisone, 0, STR, tplpath ); <br/> return strdrop + STR + "</SELECT> "; <br/>}< br/> /// <summary> <br/> // obtain all subdirectories and file functions in the specified folder <br/> /// </ summary> <br/> // <Param name = "thedir"> specify the object </Param> <br/> // <Param name = "nlevel"> default start value, it is generally 0 </param> <br/> // <Param name = "RN"> input value used for superposition, it is generally null </param> <br/> /// <Param name = "tplpath"> default Template Name </param> <br/> /// <returns> </returns> <br/> Public static string listtreeshow (directoryinfo thedir, int nlevel, string RN, string tplpath) // recursive directory file <br/> {<br/> directoryinfo [] subdirectories = thedir. getdirectories (); // get the directory <br/> foreach (direct Oryinfo dirinfo in subdirectories) <br/>{< br/> rn + = "<option value =/" "+ dirinfo. name. tostring () + "/" "; <br/> If (tplpath. tolower () = dirinfo. name. tostring (). tolower () <br/>{< br/> rn + = "selected"; <br/>}< br/> rn + = "> "; <br/> If (nlevel = 0) <br/> {<br/> rn + = "success "; <br/>}< br/> else <br/> {<br/> string _ S = ""; <br/> for (INT I = 1; I <= nlevel; I ++) <br/>{< br/> _ S + = "│"; <br/ >}< Br/> rn + = _ S + "Hangzhou"; <br/>}< br/> rn + = "" + dirinfo. name. tostring () + "</option>"; </P> <p> fileinfo [] fileinfo = dirinfo. getfiles (); // file in the directory <br/> foreach (fileinfo finfo in fileinfo) <br/> {<br/> rn + = "<option value =/" "+ dirinfo. name. tostring () + "/" + finfo. name. tostring () + "/" "; <br/> If (tplpath. tolower () = finfo. name. tostring (). tolower () <br/>{< br/> rn + = "selected"; <br/>}< br/> Rn + = ">"; <br/> If (nlevel = 0) <br/> {<br/> rn + = "│ ├ "; <br/>}< br/> else <br/> {<br/> string _ f = ""; <br/> for (INT I = 1; I <= nlevel; I ++) <br/>{< br/> _ F + = "│ "; <br/>}< br/> rn + = _ F + "│ ├"; <br/>}< br/> rn + = finfo. name. tostring () + "</option>"; <br/>}< br/> Rn = listtreeshow (dirinfo, nlevel + 1, RN, tplpath ); </P> <p >}< br/> return rn; <br/>}< br/> # endregion <br/> # region obtained Folder size <br/> /******************************** * ******* <br/> * Function Name: getdirectorylength (string dirpath) <br/> * Function Description: Get the folder size <br/> * parameter number: dirpath: detailed folder path <br/> * Call display column: <br/> * string Path = server. mappath ("templates"); <br/> * response. write (EC. fileobj. getdirectorylength (PATH )); <br/> ************************************ *****/<br/> /// <summary> <br/> // obtain the folder size <br/> /// </Summary> <br/> /// <par Am name = "dirpath"> folder path </param> <br/> // <returns> </returns> <br/> Public static long getdirectorylength (string dirpath) <br/>{< br/> If (! Directory. exists (dirpath) <br/> return 0; <br/> long Len = 0; <br/> directoryinfo di = new directoryinfo (dirpath ); <br/> foreach (fileinfo fi in Di. getfiles () <br/>{< br/> Len + = Fi. length; <br/>}< br/> directoryinfo [] Dis = di. getdirectories (); <br/> If (DIS. length> 0) <br/>{< br/> for (INT I = 0; I <dis. length; I ++) <br/>{< br/> Len + = getdirectorylength (DIS [I]. fullname); <br/>}< br/> return Len; <br/>}< br/> # endregion <br/> # obtain detailed attributes of a specified file by region <br/> /************* * ************************* <br/> * Function Name: getfileattibe (string filepath) <br/> * Function Description: obtains the detailed attributes of a specified file. <br/> * parameter: filepath: detailed file path <br/> * indicates the call column: <br/> * string file = server. mappath ("robots.txt"); <br/> * response. write (EC. fileobj. getfileattibe (File )); <br/> ************************************ *****/<br/> /// <summary> <br/> // obtain the detailed attributes of a specified file <br/> /// </Summary> <br/> /// <Param name = "filepath"> detailed file path </param> <br/> /// <returns> </returns> <br/> Public static string getfileattibe (string filepath) <br/>{< br/> string STR = ""; <br/> system. io. fileinfo objfi = new system. io. fileinfo (filepath); <br/> STR + = "Detailed path:" + objfi. fullname + "<br> file name:" + objfi. name + "<br> file length:" + objfi. length. tostring () + "byte <br> Creation Time" + objfi. creationtime. tostring () + "<br> last access time:" + objfi. lastaccesstime. tostring () + "<br> modification time:" + objfi. lastwritetime. tostring () + "<br> directory:" + objfi. directoryname + "<br> Extension:" + objfi. extension; <br/> return STR; <br/>}< br/> # endregion <br/>}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.