FileSystemWatcher

  • Thread starter Thread starter Ken
  • Start date Start date
K

Ken

I noticed and inconsistency in the FileSystemWatcher class. There is a
constructor that allows you to initialize the instance with the path to be
watched. It throws an exception if the path parameter is null, an empty
string, contains invalid path characters or the path can not be found. If
you instantiate with the default constructor and then set the Path via the
property, it will not throw an exception if you set it to null, e.g.
instance.Path = null;. The other three conditions are trapped in the
property Set.

Thank You
Ken
 
Hi Ken,

Based on your description I can reproduce the problem.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = null;
will not throw exception while
FileSystemWatcher watcher = new FileSystemWatcher(null);
will throw exception.

I am researching the issue, if I have new information I will update with
you ASAP.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hello Ken,

Thanks for posting in the group and your feedback.

We did a quick research on it and checked the IL code of Set_Path property.

public void set_Path(string value)
{
object[] array1;
value = ((value == null) ? string.Empty : value);
if (string.Compare(this.directory, value, 1, CultureInfo.InvariantCulture)
== 0)
{
goto L_0099;
}
if (base.DesignMode)
{
if ((value.IndexOfAny(FileSystemWatcher.wildcards) == -1) &&
(value.IndexOfAny(Path.InvalidPathChars) == -1))
{
goto L_0085;
}
array1 = new object[1];
array1[0] = value;
throw new ArgumentException(SR.GetString("InvalidDirName", array1));

}
if (!Directory.Exists(value))
{
array1 = new object[1];
array1[0] = value;
throw new ArgumentException(SR.GetString("InvalidDirName", array1));

}
L_0085:
this.directory = value;
this.readGranted = false;
this.Restart();

L_0099:
return;
}

So if Set_Path(null), it will change the path to string.Empty.

Based on my testing,

1) If we transfer null to FileSystemWatcher constructor, it will throw
ArgumentNullException
which is the same as
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemiofilesystemwa
tcherclassctortopic2.asp?frame=true

2) If we use
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = null;
it won't cause any exception, which is somewhat different from MSDN
mentioned.
If we add watcher.EnableRaisingEvents = true; // the line will throw the
exception as the doc said.
then an exception will be thrown.

This NULL part of MSDN is new added. It didn't exist in July version of
MSDN. I will ping our product group to give the feedback to them.

Thanks again for your valueful feedback. If you have any more concerns on
it, pleaes feel free to post here.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Ken,

The MSDN doc on this topic is not quite clear. I have contacted our product
group, MSDN content group and open a service request for this issue. Thanks
very much for your feedback on it.

If you have any more concerns on it, please feel free to post here.

Thanks again for participating the community.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top