Skip to content

Commit f356714

Browse files
committed
Handle when the 'hidden' attribute changes
1 parent 26c6c87 commit f356714

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/ManagedShell.ShellFolders/ChangeWatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public ChangeWatcher(List<string> pathList, FileSystemEventHandler changedEventH
3131
{
3232
FileSystemWatcher watcher = new FileSystemWatcher(path)
3333
{
34-
NotifyFilter = NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastWrite | NotifyFilters.Size
34+
NotifyFilter = NotifyFilters.Attributes | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastWrite | NotifyFilters.Size
3535
};
3636

3737
watcher.Changed += _changedEventHandler;

src/ManagedShell.ShellFolders/ShellFolder.cs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,21 @@ private void ChangedEventHandler(object sender, FileSystemEventArgs e)
257257
if (file.Path == e.FullPath)
258258
{
259259
exists = true;
260-
file.Refresh();
260+
261+
if (FileIsHidden(file.Path))
262+
{
263+
RemoveFile(file.Path);
264+
}
265+
else
266+
{
267+
file.Refresh();
268+
}
261269

262270
break;
263271
}
264272
}
265273

266-
if (!exists)
274+
if (!exists && !FileIsHidden(e.FullPath))
267275
{
268276
AddFile(e.FullPath);
269277
}
@@ -276,7 +284,7 @@ private void CreatedEventHandler(object sender, FileSystemEventArgs e)
276284
{
277285
ShellLogger.Info($"ShellFolder: Item {e.ChangeType}: {e.Name} ({e.FullPath})");
278286

279-
if (!FileExists(e.FullPath))
287+
if (!FileExists(e.FullPath) && !FileIsHidden(e.FullPath))
280288
{
281289
AddFile(e.FullPath);
282290
}
@@ -301,7 +309,7 @@ private void RenamedEventHandler(object sender, RenamedEventArgs e)
301309

302310
int existing = RemoveFile(e.OldFullPath);
303311

304-
if (!FileExists(e.FullPath))
312+
if (!FileExists(e.FullPath) && !FileIsHidden(e.FullPath))
305313
{
306314
AddFile(e.FullPath, existing);
307315
}
@@ -405,6 +413,23 @@ private bool FileExists(string parsingName)
405413

406414
return exists;
407415
}
416+
417+
private bool FileIsHidden(string parsingName)
418+
{
419+
try
420+
{
421+
FileAttributes attributes = File.GetAttributes(parsingName);
422+
if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
423+
{
424+
return true;
425+
}
426+
}
427+
catch (Exception ex)
428+
{
429+
ShellLogger.Warning($"ShellFolder: Unable to retrieve attributes for {parsingName}: {ex.Message}");
430+
}
431+
return false;
432+
}
408433
#endregion
409434

410435
public new void Dispose()

0 commit comments

Comments
 (0)