Obtain file attributes in C #

Source: Internet
Author: User

In Explorer, select the "details" view, and you can see many file attributes, the resolution of a piece, the artist of MP3, the bit rate, the resolution of a video file, and so on, sometimes it may be difficult to obtain in C #. Image is used to compare the chip resolution. fromFile obtains the Image class before obtaining resolution data. However, loading images into the memory will increase the memory overhead and consume more time. So today we will talk about how to get the detailed attributes of a file in C.

Add reference

Add references to C: \ Windows \ System32 \ shell32.dll in your project, we also used this dynamic link library in the previous issue "C # using SHFileOperation to call the copy file dialog box for Windows". However, we used the ing method and now we reference it, directly.

 Shell32;

Code Implementation

Dictionary <,> GetProperties ((! FileNotFoundException (===<,> Properties = Dictionary <,> I = (key = folder. GetDetailsOf (value = ++GetProperties

This method returns all attribute values. On my Win7 Pro 64bit, 287 attributes are returned! As you can imagine, the information is rich, but the speed is slow enough.

As you can see, the code above uses a loop where I is used to index attribute names and attribute values. Then, can we directly use subscript to obtain the desired attribute instead of loop? The Code is as follows:

GetPropertyByIndex (filePath ,(! FileNotFoundException (=== value = key = folder. GetDetailsOf (===GetPropertyByIndex

In my system environment, the resolution subscript "size" is 31, so I only need to GetPropertyByIndex (@ "D: \ test.jpg", 31) to get the resolution information. Note that the subscript of the "size" attribute is not necessarily the same in different Windows versions (XP, Vista, Win7, Win2003, etc.

OK. We also noticed that each attribute has a corresponding "attribute name". So, can we get the attribute value through the attribute name? This is more secure than using subscript. The Code is as follows:

GetProperty (filePath ,(! FileNotFoundException (=== value = I = (key = folder. GetDetailsOf (====++GetProperty

This method was first written by me. By adding the attribute name in the while clause, the corresponding attribute value is returned until the corresponding attribute name is found.

However, this method is not concise enough. The "size" attribute is 31, which means that every time 31 cycles are required to get the value I want, if the attribute name I want to obtain is marked as 287 (see the above), the number of times will be more, so I made some optimizations to the Code:

Dictionary <,> _ propertyIndex = GetPropertyEx (filePath, (_ propertyIndex = propertyNameLow = index = <,> propertyIndex = Dictionary <,> tempFile = <,> allProperty = (allProperty! = Index = (item ++ =GetPropertyEx

_ PropertyIndex is used to store the attribute name and its subscript. Dictionary is used because the time complexity of _ propertyIndex [key] is O (1 ). Then, find the subscript corresponding to the property name in the GetPropertyEx method, and return the property value of the property name. The InitPropertyIndex method is called only once.

Now we can get the attribute value through the attribute name, so there should be no problem between different systems, right?

Not necessarily, because you must have thought of it. For an English windows system, its attribute name does not have a "size ", it should correspond to "Resolution" and so on (I don't have an English version system, so I just guess), and there won't be a "Name" attribute, but "Name ";

To sum up,

Method Name

Applicable

Not applicable

GetPropertyByIndex

Systems in different languages

Systems of different versions

GetPropertyEx

Systems of different versions

Systems in different languages

Therefore, select the appropriate method based on the possible running environment of your program;

Think about it again: What should I do if I want to be able to make the system generic in different languages and versions?

What I think of now is to establish the correspondence between the same attribute names in different languages, for example, "size" corresponds to "Resolution", and then obtain the system language in the code, translate the attribute name into this language, and you can find the corresponding attribute value through the translated attribute name. You are welcome to discuss the implementation of this method or a better method.

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.