File and folder operations-File, Directory, FileInfo, DirectoryInfo, and cfilefileinfo

Source: Internet
Author: User

File and folder operations-File, Directory, FileInfo, DirectoryInfo, and cfilefileinfo

File and folder operations:

C/S: WinForm can operate the Client file Client Server
B/S: Brower Server

Namespace: using system. IO;

1. File class:

Create: File. Create (PATH); Create File, return FileStream

FileStream fs = File. Create (PATH); disable it later; otherwise, the File cannot be opened, fs. close ();

Delete: File. Delete (PATH); no return value

Copy File: File. Copy (source File, target File );

Cut File: File. Move (source File path, target path );

Determine whether a File Exists: File. Exists (PATH); returns a Boolean value. true indicates that the File already Exists.

File encryption: File. Encrypt (); File. Decrypt (); decryption

File. GetCreationTime (PATH); get the creation time, return the DateTime type SetCreationTime (path, DateTime type); Modify the creation time

File. GetLastAccessTime (PATH); last access time, return the DateTime type SetLastAccessTime (path, DateTime type); Modify the access time

File. GetLastWriteTime (PATH); last modification time, return the DateTime type SetLastWriteTime (path, DateTime type); modification time


2. Directory class, Directory (folder)

Directory. CreateDirectory (PATH); create Directory

Directory. Delete (PATH); Delete Directory

Directory. Exists (PATH); whether the Directory Exists

Get and set at three times

Directory. GetDirectories (PATH); obtain the subdirectory and return a string Array

Directory. GetFiles (PATH); get the sub-file! Name !, Returns the string array, string [] s = Directory. GetFiles (PATH );

Directory. GetDirectoryRoot (PATH); get the root Directory

Directory. GetParent (PATH); obtain the Directory of the upper-level

Bytes ------------------------------------------------------------------------------------------------------

FileInfo class

It is an instance method and can be used only when the new object is created. The above is a static File method.

Create a file: FileInfo f = new FileInfo (PATH); FileStream s = f. Create (); s. Close ();

Delete a file: FileInfo f = new FileInfo (PATH); f. Delete ();

Copy a file: FileInfo f = new FileInfo (PATH); f. CopyTo (target path, whether to overwrite (true indicates overwrite ));

Move the file: FileInfo f = new FileInfo (PATH); f. MoveTo (target path );

Whether the file Exists: FileInfo f = new FileInfo (PATH); bool B = f. Exists; Boolean, attribute

Get the file name: FileInfo f = new FileInfo (PATH); string s = f. FullName; attribute, file name with Path

Obtain the creation time: DateTime d = f. CreationTime. All three times are the same and all are attributes.

Set the creation time: f. CreationTime = DateTime. Now. AddDays (100 );

Get file size: f. Length


DirectoryInfo class

Create directory: DirectoryInfo d = new DirectoryInfo (PATH); d. Create ();

Delete Directory: d. Delete ();

Move Directory: d. MoveTo (target path );

Whether the directory Exists: bool B = d. Exists;

Obtain the full directory name: d. FullName;

Get the sub-file! Object Information! : FileInfo [] f = d. GetFiles (); an array of file objects is returned. For more information, d. GetFiles ("*. exe") only obtains files of exe.

Obtain the subdirectory: DirectoryInfo [] dr = d. GetDirectories ();

Instance display: Read directory size

Private long size = 0;
Private long DirSize (string path)
{
DirectoryInfo d = new DirectoryInfo (path );
FileInfo [] f = d. GetFiles ();
Foreach (FileInfo wj in f)
{
Size + = wj. Length;
}

DirectoryInfo [] dr = d. GetDirectories ();
If (dr. Count ()> 0)
{
Foreach (DirectoryInfo wjj in dr)
{
DirSize (wjj. FullName );
}
}

Return size;
}

Number of folders in the read directory:

Private int dcount = 0;
Private int DirCount (string path)
{
DirectoryInfo d = new DirectoryInfo (path );

DirectoryInfo [] dr = d. GetDirectories ();

If (dr. Count ()> 0)
{
Foreach (DirectoryInfo wjj in dr)
{
DirCount (wjj. FullName );
}
}

Dcount + = dr. Count ();

Return dcount;
}

Number of Files Read:

Private int count = 0;
Private int FileCount (string path)
{
DirectoryInfo d = new DirectoryInfo (path );
FileInfo [] f = d. GetFiles ();

DirectoryInfo [] dr = d. GetDirectories ();
If (dr. Count ()> 0)
{
Foreach (DirectoryInfo wjj in dr)
{
FileCount (wjj. FullName );
}
}

Count + = f. Count ();

Return count;
}

Query the number of all files in a folder by traversing the set. The number of folders:

Private int fcount = 0;
Private int FileCount (string path)
{
// Create a folder information object
DirectoryInfo dwjj = new DirectoryInfo (path );

// Obtain the number of files in the current folder
Fcount + = dwjj. GetFiles (). Length;

// Retrieve all folders in the current directory
Foreach (DirectoryInfo d in dwjj. GetDirectories ())
{
FileCount (d. FullName );
}

Return fcount;

}

Private int dcount = 0;
Private int DirCount (string path)
{
// Create a folder information object
DirectoryInfo d = new DirectoryInfo (path );

// Retrieve all the folders in this directory
DirectoryInfo [] df = d. GetDirectories ();

// Accumulate the number of folders
Dcount + = df. Length;

// Traverse all folders
Foreach (DirectoryInfo w in df)
{
DirCount (w. FullName );
}

Return dcount;
}

 

Related Article

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.