C # get file basic information

Source: Internet
Author: User

Scenario Description:

You need to get the file information for the specified path and then manipulate the file

First, obtain the file information, form a DataTable information.

    /// <summary>    ///gets the file information in the specified directory/// </summary>    /// <param name= "path" >Absolute Path</param>    /// <returns></returns>     PublicDataTable GetInfo (stringpath) {        stringFileName ="", FileType ="", capacity ="", EditTime ="", dir ="", File =""; DataTable DT=NewDataTable (); Dt. Columns.Add ("FileName"); Dt. Columns.Add ("FileType"); Dt. Columns.Add ("capacity"); Dt. Columns.Add ("EditTime"); Dt. Columns.Add ("dir"); Dt. Columns.Add ("file"); DirectoryInfo di=NewDirectoryInfo (path); foreach(FileSystemInfo FSIinchdi. Getfilesysteminfos ()) {DataRow Nrow=dt.            NewRow (); if(FSI isFileInfo)//if the file{FileInfo fi=(FileInfo) FSI; FileName= fi. Name;//Get file nameFile = fi. FullName;//get the full catalog                if(FI. Extension.length <1)//if the extension is less than one{FileType=""; }                Else{FileType= fi. Extension.remove (0,1);//get file name extension} dir=fi.                Directory.tostring (); Capacity= Pub. Filecountsize (Convert.ToInt32 (FI. Length.tostring ()));//Calculate File SizeEditTime = fi. Lastwritetime.tostring ();//gets the last modified time            }            Else  //if it is a folder{di=(DirectoryInfo) FSI; FileName=di.                Name; Dir=di.                FullName; File=""; FileType="folder"; Capacity=""; EditTime=di.            Lastwritetime.tostring (); } nrow["FileName"] =FileName; nrow["FileType"] =FileType; nrow["capacity"] =capacity; nrow["EditTime"] =EditTime; nrow["dir"] =dir; nrow["file"] =file; Dt.        Rows.Add (Nrow); } dt. Defaultview.sort="edittime desc"; returnDT; }

Second, call the GetInfo method, display the file information, the result is as follows

C # get file basic information

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.