A extension method of the file information class that returns a formatted version of the file size. For example: 1 GB or B and it at Max it would have two decimals. Add the following code to the common static class of the same namespace, create a new FileInfo, and invoke GetFileSize.
C # Get file size, create time, file information, FileInfo class's property sheet OpenFileDialog openFileDialog1 = new OpenFileDialog ();
The code is as follows |
Copy Code |
if ( Openfiledialog1.showdialog () = = DialogResult.OK) { openfiledialog1.filename; System.IO.FileInfo file = new System.IO.FileInfo (openfiledialog1.filename); file. name;//file name file. Length.tostring ();//Size ", file. Lastaccesstime.tostring ()//Last access time file. Lastwritetime.tostring ()//Final modification time file. directoryname;//path } |
Get size and Convert unit MB
The code is as follows |
Copy Code |
<summary> Gets a files formatted size. </summary> <param name= "File" >the file to return size of.</param> <returns></returns> public static string GetFileSize (this FileInfo file) { Try { Determine all file sizes Double sizeinbytes = file. Length; Double sizeinkbytes = Math.Round ((sizeinbytes/1024)); Double sizeinmbytes = Math.Round ((sizeinkbytes/1024)); Double sizeingbytes = Math.Round ((sizeinmbytes/1024)); if (Sizeingbytes > 1) return string. Format ("{0} GB", sizeingbytes); Returns size in gigabytes else if (Sizeinmbytes > 1) return string. Format ("{0} MB", sizeinmbytes); Returns size in megabytes if less than one gigabyte else if (Sizeinkbytes > 1) return string. Format ("{0} KB", sizeinkbytes); Returns size in Kilabytes if less than one megabyte Else return string. Format ("{0} B", sizeinbytes); Returns size in bytes if less than one kilabyte } Catch {return ' error getting size ';}//catches any possible error and just returns error getting size } |
Get folder and file occupancy space
First, obtain information about the disk through the Windows API.
Calling the Windows API to get disk free space
Import Library
The code is as follows |
Copy Code |
[DllImport ("kernel32.dll", CharSet = CharSet.Auto)] static extern bool GetDiskFreeSpace ([MarshalAs (UNMANAGEDTYPE.LPTSTR)]string Rootpathname, ref int SectorsPerCluster, ref int bytespersector, ref int numberoffreeclusters, ref int totalnumbeofclusters); Here is the specific code. It's easier to write the article for the first time. <summary> Gets the size of the specified path </summary> <param name= "Dirpath" > Path </param> <returns></returns> public static long Getdirectorylength (string dirpath) { Long len = 0; Determine if the path exists (whether it is a folder) if (! Directory.Exists (Dirpath)) { Size of query file Len = FileSize (Dirpath); } Else { Define a DirectoryInfo object DirectoryInfo di = new DirectoryInfo (Dirpath); Gets the size of all files in the Di directory by using the GetFiles method foreach (FileInfo fi in Di. GetFiles ()) { Len + = fi. Length; } Gets all the folders in Di, coexisting in a new object array for recursion directoryinfo[] dis = di. GetDirectories (); if (DIS. Length > 0) { for (int i = 0; i < dis. Length; i++) { Len + + getdirectorylength (Dis[i]. FullName); } } } return Len; } <summary> Gets the space occupied by the specified path </summary> <param name= "Dirpath" > Path </param> <returns></returns> public static long Getdirectoryspace (string dirpath) { return value Long len = 0; Determine if the path exists (whether it is a folder) if (! Directory.Exists (Dirpath)) { If it is a file, call the Len = Filespace (Dirpath); } Else { Define a DirectoryInfo object DirectoryInfo di = new DirectoryInfo (Dirpath); Cluster values for this machine Long clustersize = Getclustersize (DI); Traverse the files in the directory to get total footprint foreach (FileInfo fi in Di. GetFiles ()) { File size divided by cluster, if not 0 if (FI. Length% clustersize!= 0) { Decimal res = fi. Length/clustersize; The file size divided by the cluster, the integer plus 1. The value that occupies the cluster for the file int CLU = Convert.ToInt32 (math.ceiling (res)) + 1; Long result = Clustersize * CLU; Len + = result; } Else { Yu Jo is 0, occupy space equals file size Len + = fi. Length; } } Gets all the folders in Di, coexisting in a new object array for recursion directoryinfo[] dis = di. GetDirectories (); if (DIS. Length > 0) { for (int i = 0; i < dis. Length; i++) { Len + + getdirectoryspace (Dis[i]. FullName); } } } return Len; } File size corresponding to the given path public static long FileSize (string filePath) { Defines a FileInfo object that is associated with the file that the FilePath points to to get its size FileInfo FileInfo = new FileInfo (FilePath); return fileinfo.length; } Space occupied by the corresponding file in the given path public static long Filespace (string filePath) { Long temp = 0; Defines a FileInfo object that is associated with the file that the FilePath points to to get its size FileInfo FileInfo = new FileInfo (FilePath); Long clustersize = Getclustersize (FileInfo); if (fileinfo.length% clustersize!= 0) { decimal res = fileinfo.length/clustersize; int CLU = Convert.ToInt32 (math.ceiling (res)) + 1; temp = clustersize * CLU; } Else { return fileinfo.length; } return temp; } public static DiskInfo Getdiskinfo (String rootpathname) { DiskInfo diskinfo = new DiskInfo (); int sectorspercluster = 0, BytesPerSector = 0, numberoffreeclusters = 0, totalnumberofclusters = 0; GetDiskFreeSpace (Rootpathname, ref sectorspercluster, ref bytespersector, ref numberoffreeclusters, ref Totalnumberofclusters); Number of sectors per cluster Diskinfo.sectorspercluster = SectorsPerCluster; Bytes per sector Diskinfo.bytespersector = BytesPerSector; return diskinfo; } <summary> Structure. Hard Drive information </summary> public struct DiskInfo { public string Rootpathname; Number of sectors per cluster public int sectorspercluster; Bytes per sector public int bytespersector; public int numberoffreeclusters; public int totalnumberofclusters; }///<summary> Get bytes per cluster </summary> <param name= "File" > Specify file </param> <returns></returns> public static long Getclustersize (FileInfo file) { Long clustersize = 0; DiskInfo diskinfo = new DiskInfo (); DiskInfo = getdiskinfo (file. Directory.Root.FullName); Clustersize = (Diskinfo.bytespersector * diskinfo.sectorspercluster); return clustersize; } <summary> Get bytes per cluster </summary> <param name= "dir" > specified directory </param> <returns></returns> public static Long Getclustersize (DirectoryInfo dir) { Long clustersize = 0; DiskInfo diskinfo = new DiskInfo (); DiskInfo = Getdiskinfo (dir. Root.fullname); Clustersize = (Diskinfo.bytespersector * diskinfo.sectorspercluster); return clustersize; } |