This is a reminder to get file properties in C#.
GetDetailsOf to get properties with reference to Shell32
First, Shell32 is added by reference.
Select Browse Settings -> Add to Browse in the Solution Explorer.
From the COM tab, select Microsft Shell Controls And Automation.
Shell32 has been added to the reference configuration.
Here is a sample of GetDetailsOf to get file properties directly under a folder.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace ConsoleApp_Sample1 { class Program { [STAThread] static void Main(string[] args) { String targetFolder = "D:\\test"; System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(targetFolder); System.IO.FileInfo[] files = di.GetFiles("*.*"); Shell32.Shell shell = new Shell32.Shell(); foreach (System.IO.FileInfo f in files) { var strFileName = f.FullName; Shell32.Folder objFolder = shell.NameSpace(System.IO.Path.GetDirectoryName(strFileName)); Shell32.FolderItem folderItem = objFolder.ParseName(System.IO.Path.GetFileName(strFileName)); String filename = objFolder.GetDetailsOf(folderItem, 0); Console.WriteLine(filename); } } } }
At this time, if you do not put [STAThread] on the method that calls GetDatailsOf, you will get the following error.
Cannot cast COM object of type ‘System.__ComObject’ to interface type ‘Shell32.Shell’ COM of interface with IID ‘{34936BA1-67AD-4C41-99B8-8C12DFF1E974}’ This operation failed because the following error occurred during the QueryInterface call on the component: Interface not supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))”
If you get the error “Cannot cast COM object of type ‘System.__ComObject’ to interface type ‘Shell32”.
After a short time of handing the mockup to the client, we were informed that there was an error.
InvalidCastException: Unable to cast COM object of type ‘System.__ComObject’ to interface type ‘Shell32. This operation failed because when calling QueryInterface on the COM component of the interface with IID ‘{286E6F1B-7113-4355-9562-96B7E9D64C54}’ the following error occurred: The interface is not supported. Not supported (Exception:0x80004002 (E_NOINTERFACE) from HRESULT)”.
There was no problem with the mockup Ver. 0.1 that was passed for the first time, but the error occurs with the second mockup Ver. 0.2, which was separately modified with minor modifications to layout, etc.
I wonder, it’s not that big of a fix. They are loading the same file in ver 0.1, which had no problems, and ver 0.2, which has errors. The client is Windows 10.
I couldn’t figure out the cause for a while, but as I looked into it, it turned out that the error was caused by a difference in the version of Windows on which it was compiled.
I develop on Windows 7 at home and Windows 10 on the PC I take outside. Windows 10 and Windows 7. The mockup that was giving me errors was compiled on Windows 7, so it was giving me errors on the client Window 10.
Is it a Shell32 compatibility issue in the reference configuration, Microsoft, please? So I changed the following to dynamically reference Shell32.
GetDetailsOf file property acquisition improved
[STAThread] static void GetFileProperty2() { String targetFolder = "D:\\test"; System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(targetFolder); System.IO.FileInfo[] files = di.GetFiles("*.*"); var shellAppType = Type.GetTypeFromProgID("Shell.Application"); dynamic shell = Activator.CreateInstance(shellAppType); foreach (System.IO.FileInfo f in files) { var strFileName = f.FullName; Shell32.Folder objFolder = shell.NameSpace(System.IO.Path.GetDirectoryName(strFileName)); Shell32.FolderItem folderItem = objFolder.ParseName(System.IO.Path.GetFileName(strFileName)); String filename = objFolder.GetDetailsOf(folderItem, 0); Console.WriteLine(filename); } }
Type.GetTypeFromProgID(“Shell.Application”) to dynamically get the Shell32 type, and Activator.CreateInstance to create a Shell32 instance.
GetDetailsOf property list
List of properties that can be obtained with GetDetailsOf.
*Return values for dates and times, such as updated date and time, are up to hours and minutes (e.g., “2019/10/06 10:55”). If you want to get up to seconds, use System.IO.FileInfo. As for the date and time the photo was taken, it cannot be obtained with System.IO. For details on how to obtain the date and time, please refer to Get the date and time the photo was taken down to the second.
0 Name
1 Size
2 Item Type
3 Update Date
4 Creation date
5 Access Date
6 Attributes
7 Offline Status
8 Available offline
9 Recognized type
10 Owner
11 Classification
12 Date and time of shooting
13 Participating artists
14 Album
15 Year
16 Genre
17 Conductor
18 Tags
19 Rating
20 Author
21 Title
22 Subject
23 Category
24 Comment
25 Copyright
26 Track Number
27 Length
28 Bit Rate
29 Protection
30 Camera Model
31 Size
32 Camera Manufacturer
33 Company
34 File description
35 Program name
36 Duration
37 Online
38 Recurrence
39 Location
40 Optional Attendee Address
41 Optional attendee
42 Host Address
43 Host name
44 Alarm time
45 Mandatory attendee address
46 Required attendee
47 Resources
48 Meeting Status
49 Availability information
50 Total size
51 Account Name
52 Progress
53 Computer
54 Anniversary
55 Secretary’s name
56 Secretary’s Phone
57 Date of birth
58 Address (Company)
59 City (Company)
60 Country/Region (Company)
61 P.O. Box (Company)
62 Postal code (Company)
63 State/Province (Company)
64 Street Address (Company)
65 Fax Number (Company)
66 Company Home Page
67 Company Phone
68 Callback Number
69 Car Phone
70 Children
71 Company Representative Phone
72 Department
73 E-mail address
74 Email 2
75 Email 3
76 Email List
77 E-mail display name
78 Title 2
79 First name
80 First Name
81 Gender
82 First Name2
83 Hobby
84 Address (Home)
85 City (at home)
86 Country/Region (Home)
87 P.O. Box (at home)
88 Postal code (home)
89 State/Province (home)
90 Street Address (Home)
91 Home fax
92 Home Phone
93 IM address
94 Initials
95 Job Title
96 Label
97 Last name
98 Mailing Address
99 Middle Name
100 Cell Phone
101 Nickname
102 Work Address
103 Address (Other)
104 Other City/Village
105 Other Country/Region
106 Other P.O. Box
107 Other Postal Code
108 Other State/Province
109 Other street address
110 Other pager
111 Title
112 City (mailing address)
113 Country/Region
114 P.O. Box (Mailing Address)
115 Postal code (Mailing address)
116 State/Province
117 Street Address (Mailing Address)
118 Primary Email
119 Regular Phone
120 Occupation
121 Spouse
122 Suffix
123 TTY/TTD Telephone
124 Telex
125 Web page
126 Content Status
127 Content type
128 Retrieval Date
129 Archive date
130 Completion date
131 Device category
132 Connected
133 Search method
134 Friendly name
135 Local computer
136 Manufacturer
137 Model
138 Paired
139 Class
140 Status
141 State
142 Client ID
143 Co-creator
144 Content creation date
145 Last Printed Date
146 Last Save Date
147 Business Unit
148 Document ID
149 Number of pages
150 Slides
151 Total Editing Time
152 Word count
153 Due date
154 End date
155 Number of files
156 File extension
157 File name
158 File Version
159 Flag Color
160 Flag Status
161 Free space
162 Type of sharing
163 Bit Depth
164 Horizontal Resolution
165 Width
166 Vertical Resolution
167 Height
168 Importance
169 Attachment
170 Deleted
171 Encryption status
172 Flag status
173 Completed
174 Not completed
175 Open status
176 Shared
177 Producer
178 Date and Time
179 Folder name
180 Folder Path
181 Folder
182 Participant
183 Path
184 Per location
185 Type
186 Contact Name
187 History Type
188 Language
189 Date Last Viewed
190 Description
191 Link status
192 Link destination
193 URL
194 Media Creation Date
195 Release Date
196 Encoding method
197 Episode number
198 Producer
199 Publisher
200 Season Number
201 Subtitle
202 User Web URL
203 Author
204 Attachment
205 BCC Address
206 BCC address
207 CC address
208 CC address
209 Conversation ID
210 Received date and time
211 Sent date and time
212 From address
213 Sender
214 Attached files or not
215 Sender address
216 Sender name
217 Store
218 Destination address
219 To do Title
220 To address
221 Expense information
222 Album Artists
223 Sort by Album Artist
224 Album ID
225 Sort by Album
226 Sort by Participating Artists
227 Beats per minute
228 Composer
229 Sort by Composer
230 Initial Key
231 Part of compilation
232 Atmosphere
233 Part of Set
234 Duration
235 Color
236 Parental restrictions
237 Reasons for Parental Restrictions
238 Area Used
239 EXIF Version
240 Event
241 Exposure Compensation
242 Exposure Program
243 Exposure Time
244 Aperture Value
245 Flash Mode
246 Focal Length
247 35mm Focal Length
248 ISO Speed
249 Lens Manufacturer
250 Lens Model
251 Light Source
252 Maximum Aperture
253 Metering Mode
254 Orientation
255 People
256 Program’s Mode
257 Saturation
258 Subject Distance
259 White Balance
260 Priority
261 Project
262 Channel number
263 Title of this episode
264 Subtitle
265 Re-aired
266 SAP
267 Broadcast date
268 Program description
269 Recording time
270 Station call sign
271 Station name
272 Summary
273 Excerpts
274 Automatic summary
275 Ranking of searches
276 Secrecy level
277 Shared users
278 Sharing status
279 Product name
280 Product Version
281 Support Link
282 Source
283 Start Date
284 Billing Information
285 Completion
286 Job Owner
287 Sort by Title
288 Total File Size
289 Trademarks
290 Video compression
291 Director
292 Data Speed
293 Frame Height
294 Frame Rate
295 Frame Width
296 Video Orientation
297 Total Bit Rate
コメント