Well, there's always the Object.GetType() method. Depending on what
you're trying to do, often the C# "is" and "as" operators are useful.
"is" allows you to test whether an instance is a specific type, and "as"
is essentially a cast that returns null instead of throwing an exception
when the cast fails.
I don't really get how a file can be rare and common at the same thing.
You're also not clear about where you get the FileSystemInfo instance
from. As far as I know, this isn't something that the FileSystemWatcher
provides you (the event args return paths in strings, not FileSystemInfo
objects). So while there's a way to check the type of the instance, it
would probably make more sense to try to figure out why you get an
apparently incorrect type for the instance in the first place.
Without you having posted any code, it's pretty much impossible for anyone
to assist with that. But until you understand why the Attributes property
doesn't match the type of the instance, I think it's premature to work
around the issue. It may be that things are working as designed, but you
should make sure that's true first. It seems to me that there's a good
chance you're not maintaining your list of FileSystemInfo objects
correctly and it would be a bad idea to obscure that problem with a
workaround.
Pete
Fair enough.
First off the file "setup.inf" is rare in that it is only created as a
result of building an install (*.msi). But, being a developer this is
actually a common occurrence _for me_ so I keep running into this more
than your average user. It's a temporary file but the
FileSystemWatcher will see it before it gets deleted.
You are correct in asserting that FileSystemWatcher only returns a
path. I have to then do a this.Invoke() to call a method on the main
thread that refreshes the watched directory (which is stored in a
member variable so there's no need to pass it). I don't believe the
problem lies in the FileSystemWatcher (since I don't do anything but
call a Refresh() method). The following is the code snippet that I
believe is causing the conflict:
Create a DirectoryInfo for this Path
CurrentDirInfo = new DirectoryInfo(Path);
//Get an array of all the file system objects in the directory
FileSystemInfoList = CurrentDirInfo.GetFileSystemInfos();
//Loop through each object in the array...
foreach (FileSystemInfo CurrentFileSystemInfo in FileSystemInfoList)
{
FileSystemListViewItem CurrentListViewItem;
//...and determine if it is a Directory (has the
FileAttributes.Directory flag)
if ((CurrentFileSystemInfo.Attributes & FileAttributes.Directory)
== FileAttributes.Directory) //[TAG01]
{
CurrentListViewItem = new
FileSystemListViewItem((DirectoryInfo)CurrentFileSystemInfo); //
[TAG02]
}
else
{
CurrentListViewItem = new
FileSystemListViewItem((FileInfo)CurrentFileSystemInfo);
}
this.Items.Add(CurrentListViewItem);
}
The if at [TAG01] was copied directly from MSDN.
I've used Console.WriteLine to print out the FileAttributes flags and
the file "setup.inf" writes a "-1" rather than "Directory" but the
"if" statement above still evaluates to "true" for the file. When the
code at [TAG01] evaluates "true" then the code at [TAG02] obviously
throws an Illegal Cast exception when it casts the "setup.inf" file to
a DirectoryInfo.
I could get a list of DirectoryInfo objects and then a second list of
FileInfo objects but this seemed more efficient at the time.
It looks as if the "is" keyword is what I need here. A simple test of
whether the particular item is a DirectoryInfo object or a FileInfo
object. Then I don't have to rely on the FileAttributes flags at all.
Thanks,
Tom Padilla