Directoryinfo class and fileinfo for files and streams

Source: Internet
Author: User
Directoryinfo and fileinfo are directory and file functions. In addition, they make it easier to traverse the relationship between files and directories. For example, you can easily obtain the fileinfo object of files in the directory represented by the directoryinfo object.
Note that the Directory class and file class only provide methods, while the directoryinfo and fileinfo both provide methods and properties. For example, the file class has a separate getattributes () method and setattribute () method, while the fileinfo class provides the attribute that can be read and written.
Another benefit of the directoryinfo and fileinfo classes is that they share a set of attributes and methods because they are inherited from the filesysteminfo base class. In addition, the fileinfo and directoryinfo classes have several unique members (for specific methods and attributes, see msdn ).
When creating a directoryinfo object and a fileinfo object, you must specify the complete path in the constructor. As follows:

Directoryinfo mydirectory =   New Directoryinfo ( @" C: \ Temp " );
Fileinfo myfile =   New Fileinfo ( @" C: \ temp \ readme.txt " );


When you create fileinfo and directoryinfo objects, an exception occurs if the specified path format is incorrect (for example, contains invalid characters. However, the path does not need to correspond to a real physical file or directory. If you are not sure, you can use exists to check whether a file or directory exists.
If the directory or file does not exist, you can use the CREATE () method to create them. The following is an example: // Define the new directory and file.
Directoryinfo mydirectory =   New Directoryinfo ( @" C: \ temp \ test " );
Fileinfo myfile =   New Fileinfo ( @" C: \ temp \ test \ readme.txt " );
// Now create them. Order here is important.
// You can't create a file in a directory that doesn't exist yet.
Mydirectory. Create ();
Filestream stream = Myfile. Create ();
Stream. Close ();


The directoryinfo object and the fileinfo object obtain information from the file system when you first query an attribute. They do not check new information in subsequent use. If the file changes at this time, it will lead to inconsistency. If you know or suspect that the file system information of the specified object has been changed, you can call the refresh method to obtain the latest information.
The directoryinfo class does not provide any attribute for obtaining the directory size. However, you can accumulate the fileinfo. Length base value of each file in a specific directory for calculation.TheDirectory size.
Before executing this step, you need to determine whether the subdirectory size is included. The following method allows you to use either of the two methods:

Private   Static   Long Calculatedirectorysize (directoryinfo directory,
Bool Includesubdirectories)
{
Long Totalsize =   0 ;
// Add up each file.
Fileinfo [] files = Directory. getfiles ();
Foreach (Fileinfo File In Files)
{
Totalsize + = File. length;
}
// Add up each subdirectory, if required.
If (Includesubdirectories)
{
Directoryinfo [] dirs = Directory. getdirectories ();
Foreach (Directoryinfo dir In Dirs)
{
Totalsize + = Calculatedirectorysize (Dir, True );
}
}
Return Totalsize;
}

The driveinfo class is required for the information about the remaining space..

attributes
attributes of the fileinfo and directoryinfo classes represent the file system features of files or directories. Because each file and directory can have a set of attributes. The attributes attribute contains a set of fileattributes enumeration values (see msdn for details ).
to locate all the attributes of a file, you can call the tostring () method of the attributes attribute. It returns a comma-separated feature list string:

code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/

--> // This displays a string in the format" readonly, archive, encrypted "
lblinfo. text = myfile. attributes. tostring ();

When testing a single feature, bit calculation is required. For example, consider the Code of the following error:

code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/

--> If (myfile. attributes = fileattributes. readonly)
{}


the test succeeds only when the current file only contains the read-only feature. This situation is rare. To correctly detect file errors, you only need the following code:

code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/

--> If (myfile. attributes & fileattributes. readonly) ! = 0 )
{}


This test is successful because it only filters the read-only feature.
Similar logic allows you to verify that a file does not have a specific feature: If(Myfile. Attributes&Fileattributes. readonly)! = 0)
{}


You must also use bitwise operations to set features. In this case, be careful to delete the features previously set by the file:

// This sets the read-only attribute (and keeps all others as is ).
Myfile. Attributes = Myfile. Attributes | Fileattributes. readonly;
// This removes the read-only attribute (and keeps all others as is ).
Myfile. Attributes = Myfile. Attributes &   ~ Fileattributes. readonly;

Some features cannot be set through programming. For example, the encrpted feature is set by the operating system only when you use EFs (Encrypted File System) in windows.

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.