tags:

views:

33

answers:

1

I have some C# code to get information about a file through WMI, which used to make the following query:

SELECT * FROM CIM_DataFile WHERE Drive = 'C:' AND Path = '\\temp\\' AND FileName = 'testemef' AND Extension = 'txt'

I found out I can query both directories and files if I use the CIM_LogicalFile class instead:

SELECT * FROM CIM_LogicalFile WHERE Drive = 'C:' AND Path = '\\temp\\' AND FileName = 'testemef' AND Extension = 'txt'

I devised a roundabout way to determine whether the instance returned is a file or a directory: if I get an exception trying to access the FileSize property, it's a directory; if not, it's a file. I don't like this method, for there can be things other than directories and files. (Named pipes? Symlinks?)

Unfortunately, there's not any unique property in the CIM_Directory class that doesn't exist in the base CIM_LogicalFile class, so for now I'm reduced to knowing that something "is not a file", rather than "is a directory".

Is there a more airtight way in WMI to establish that a pathspec that's not a file is in fact a directory, rather than some other exotic entity?

+1  A: 

Check the object's ClassPath.ClassName property (managed wrapper for the WMI system property __CLASS) - it returns Win32_Directory for folders and CIM_DataFile for files.

Helen
That did the trick. Thanks!
JCCyC