Skip to content

Commit 421306d

Browse files
MarcoRossignoliDan Moseley
authored andcommitted
Fix misleading exception message, cleanup code (dotnet#25211)
* Fix misleading exception message, cleanup code #24817 * Fix misleading exception message, cleanup code #24817 * Fix misleading exception message, cleanup code #24817 * Fix misleading exception message, cleanup code #24817 * Fix misleading exception message, cleanup code #24817 * Revert "Fix misleading exception message, cleanup code #24817" This reverts commit 87e87a1. * Revert "Fix misleading exception message, cleanup code #24817" This reverts commit 05f4a5a. * Revert "Revert "Fix misleading exception message, cleanup code #24817"" This reverts commit 4e35169. * Fix misleading exception message, cleanup code. fixes #24817 * Fix misleading exception message, cleanup code. fixes #24817 * Fix misleading exception message, cleanup code. fixes #24817 * Fix misleading exception message, cleanup code. fixes #24817 * Fix misleading exception message, cleanup code. fixes #24817 * Fix misleading exception message, cleanup code dotnet#25211
1 parent 5e08586 commit 421306d

3 files changed

Lines changed: 35 additions & 7 deletions

File tree

src/System.IO.FileSystem.Watcher/src/Resources/Strings.resx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@
129129
<data name="InvalidDirName" xml:space="preserve">
130130
<value>The directory name {0} is invalid.</value>
131131
</data>
132+
<data name="InvalidDirName_NotExists" xml:space="preserve">
133+
<value>The directory name '{0}' does not exist.</value>
134+
</data>
132135
<data name="InvalidEnumArgument" xml:space="preserve">
133136
<value>The value of argument '{0}' ({1}) is invalid for Enum type '{2}'.</value>
134137
</data>
@@ -189,4 +192,4 @@
189192
<data name="IO_PathTooLong_Path" xml:space="preserve">
190193
<value>The path '{0}' is too long, or a component of the specified path is too long.</value>
191194
</data>
192-
</root>
195+
</root>

src/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,12 @@ public FileSystemWatcher(string path, string filter)
105105
throw new ArgumentNullException(nameof(filter));
106106

107107
// Early check for directory parameter so that an exception can be thrown as early as possible.
108-
if (path.Length == 0 || !Directory.Exists(path))
108+
if (path.Length == 0)
109109
throw new ArgumentException(SR.Format(SR.InvalidDirName, path), nameof(path));
110110

111+
if (!Directory.Exists(path))
112+
throw new ArgumentException(SR.Format(SR.InvalidDirName_NotExists, path), nameof(path));
113+
111114
_directory = path;
112115
_filter = filter;
113116
}
@@ -150,13 +153,13 @@ public bool EnableRaisingEvents
150153
{
151154
return;
152155
}
153-
156+
154157
if (IsSuspended())
155158
{
156159
_enabled = value; // Alert the Component to start watching for events when EndInit is called.
157160
}
158161
else
159-
{
162+
{
160163
if (value)
161164
{
162165
StartRaisingEventsIfNotDisposed(); // will set _enabled to true once successfully started
@@ -416,7 +419,7 @@ private void NotifyRenameEventArgs(WatcherChangeTypes action, string name, strin
416419
{
417420
// filter if there's no handler or neither new name or old name match a specified pattern
418421
RenamedEventHandler handler = _onRenamedHandler;
419-
if (handler != null &&
422+
if (handler != null &&
420423
(MatchPattern(name) || MatchPattern(oldName)))
421424
{
422425
handler(this, new RenamedEventArgs(action, _directory, name, oldName));
@@ -525,7 +528,7 @@ protected void OnRenamed(RenamedEventArgs e)
525528
}
526529
}
527530

528-
public WaitForChangedResult WaitForChanged(WatcherChangeTypes changeType) =>
531+
public WaitForChangedResult WaitForChanged(WatcherChangeTypes changeType) =>
529532
WaitForChanged(changeType, Timeout.Infinite);
530533

531534
public WaitForChangedResult WaitForChanged(WatcherChangeTypes changeType, int timeout)

src/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Directory.Create.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@ namespace System.IO.Tests
99
{
1010
public class Directory_Create_Tests : FileSystemWatcherTest
1111
{
12+
[Fact]
13+
public void FileSystemWatcher_Directory_EmptyPath()
14+
{
15+
Assert.Throws<ArgumentException>(() =>
16+
{
17+
using (var watcher = new FileSystemWatcher(""))
18+
{
19+
}
20+
});
21+
}
22+
23+
[Fact]
24+
public void FileSystemWatcher_Directory_PathNotExists()
25+
{
26+
Assert.Throws<ArgumentException>(() =>
27+
{
28+
using (var watcher = new FileSystemWatcher(GetTestFilePath()))
29+
{
30+
}
31+
});
32+
}
33+
1234
[Fact]
1335
public void FileSystemWatcher_Directory_Create()
1436
{
@@ -81,4 +103,4 @@ public void FileSystemWatcher_Directory_Create_SymLink()
81103
}
82104
}
83105
}
84-
}
106+
}

0 commit comments

Comments
 (0)