Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.

Commit 6bbd234

Browse files
committed
- ListItemを右クリックするとエラーが発生する問題を修正。 - アクセス不可能なフォルダにアクセスできないように。
1 parent bb7971b commit 6bbd234

3 files changed

Lines changed: 112 additions & 31 deletions

File tree

YMM4FileExplorer/FileExplorerControl.xaml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
<UserControl.Resources>
1616
<helpers:BytesToHumanReadableStringConverter x:Key="BytesConverter"/>
1717

18+
<ContextMenu x:Key="ItemContextMenu">
19+
<MenuItem Header="大きく表示" Click="ShowLargePreview_Click"/>
20+
</ContextMenu>
21+
1822
<!-- 詳細ListView -->
1923
<Style x:Key="DetailsViewItemContainerStyle" TargetType="ListViewItem">
2024
<Setter Property="ContextMenu" Value="{StaticResource ItemContextMenu}"/>
@@ -90,11 +94,7 @@
9094
</Setter.Value>
9195
</Setter>
9296
</Style>
93-
94-
<ContextMenu x:Key="ItemContextMenu">
95-
<MenuItem Header="大きく表示" Click="ShowLargePreview_Click"/>
96-
</ContextMenu>
97-
97+
9898
<!-- ProgressBar -->
9999
<Style x:Key="CircularProgressBar" TargetType="ProgressBar">
100100
<Setter Property="Template">
@@ -351,9 +351,8 @@
351351
Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"
352352
Visibility="Collapsed"/>
353353

354-
<DockPanel x:Name="LargePreviewPane"
354+
<DockPanel x:Name="LargePreviewPanel"
355355
Grid.Row="1" Grid.Column="4"
356-
MinWidth="200"
357356
Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"
358357
Visibility="Collapsed">
359358
<Border BorderBrush="{DynamicResource {x:Static theme:YMM4Colors.IconBrushKey}}" BorderThickness="0.5" Margin="5,0,5,5">

YMM4FileExplorer/FileExplorerControl.xaml.cs

Lines changed: 105 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public FileExplorerControl(string initialPath = "C:\\")
6565
FileExplorerControl_Loaded();
6666
this.Unloaded += FileExplorerControl_Unloaded;
6767

68-
GoHomeCommand = new ActionCommand(_=> true, async _ =>
68+
GoHomeCommand = new ActionCommand(_ => true, async _ =>
6969
{
7070
string? targetPath = string.IsNullOrEmpty(_currentDirectory)
7171
? _initialPath
@@ -296,6 +296,18 @@ private async Task ExpandNodeAsync(TreeViewItem item)
296296

297297
foreach (var dir in directories)
298298
{
299+
if (!CanAccessDirectory(dir.FullName))
300+
{
301+
var inaccessibleItem = new TreeViewItem
302+
{
303+
Header = await CreateTreeViewItemHeaderAsync(dir.Name, dir.FullName),
304+
Tag = dir.FullName,
305+
IsEnabled = false // 選択不可
306+
};
307+
item.Items.Add(inaccessibleItem);
308+
continue;
309+
}
310+
299311
var subItem = new TreeViewItem
300312
{
301313
Header = await CreateTreeViewItemHeaderAsync(dir.Name, dir.FullName),
@@ -304,9 +316,7 @@ private async Task ExpandNodeAsync(TreeViewItem item)
304316

305317
try
306318
{
307-
bool hasSubDirs = await Task.Run(
308-
() => Directory.EnumerateDirectories(dir.FullName).Any()
309-
);
319+
bool hasSubDirs = CanAccessDirectory(dir.FullName) && Directory.EnumerateDirectories(dir.FullName).Any();
310320

311321
if (hasSubDirs)
312322
{
@@ -341,7 +351,6 @@ private async Task UpdateTreeView(string path)
341351
{
342352
if (!Directory.Exists(path)) return;
343353

344-
345354
var dirInfo = new DirectoryInfo(path);
346355
if (dirInfo.Parent == null)
347356
{
@@ -373,10 +382,50 @@ private async Task UpdateTreeView(string path)
373382

374383
try
375384
{
376-
var directories = await Task.Run(() =>
377-
dirInfo.GetDirectories()
378-
.Where(dir => FileExplorerSettings.Default.ShowHiddenFiles || !dir.Attributes.HasFlag(FileAttributes.Hidden))
379-
.ToList());
385+
var directories = new List<DirectoryInfo>();
386+
387+
try
388+
{
389+
foreach (var dir in dirInfo.EnumerateDirectories())
390+
{
391+
if (!CanAccessDirectory(dir.FullName))
392+
{
393+
var inaccessibleItem = new TreeViewItem
394+
{
395+
Header = await CreateTreeViewItemHeaderAsync(dir.Name, dir.FullName),
396+
Tag = dir.FullName,
397+
IsEnabled = false // 選択できないように
398+
};
399+
rootItem.Items.Add(inaccessibleItem);
400+
continue;
401+
}
402+
403+
// 通常のフォルダ
404+
var subItem = new TreeViewItem
405+
{
406+
Header = await CreateTreeViewItemHeaderAsync(dir.Name, dir.FullName),
407+
Tag = dir.FullName
408+
};
409+
410+
if (CanAccessDirectory(dir.FullName))
411+
{
412+
try
413+
{
414+
if (Directory.EnumerateDirectories(dir.FullName).Any())
415+
subItem.Items.Add(null);
416+
}
417+
catch { /* アクセス不可は無視 */ }
418+
}
419+
420+
subItem.Expanded += Folder_Expanded;
421+
rootItem.Items.Add(subItem);
422+
423+
}
424+
}
425+
catch (UnauthorizedAccessException)
426+
{
427+
Debug.WriteLine($"ディレクトリ列挙自体にアクセスできません: {dirInfo.FullName}");
428+
}
380429

381430
foreach (var dir in directories)
382431
{
@@ -460,18 +509,30 @@ private async Task HandlePathSelection(string path, bool addToHistory = true)
460509
if (!Directory.Exists(path)) return;
461510

462511
_watcher?.Dispose();
512+
_watcher = null;
513+
463514
SearchTextBox.Text = string.Empty;
464515
_currentDirectory = path;
465516
await LoadFilesAsync(path);
466517

467-
_watcher = new FileSystemWatcher(path)
518+
if (CanAccessDirectory(path))
468519
{
469-
NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName,
470-
EnableRaisingEvents = true,
471-
};
472-
_watcher.Created += OnFileSystemChanged;
473-
_watcher.Deleted += OnFileSystemChanged;
474-
_watcher.Renamed += OnFileSystemChanged;
520+
try
521+
{
522+
_watcher = new FileSystemWatcher(path)
523+
{
524+
NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName,
525+
EnableRaisingEvents = true,
526+
};
527+
_watcher.Created += OnFileSystemChanged;
528+
_watcher.Deleted += OnFileSystemChanged;
529+
_watcher.Renamed += OnFileSystemChanged;
530+
}
531+
catch (Exception ex)
532+
{
533+
_watcher = null;
534+
}
535+
}
475536

476537
PathChanged?.Invoke(path);
477538
if (addToHistory && !_isNavigatingViaHistory)
@@ -480,6 +541,19 @@ private async Task HandlePathSelection(string path, bool addToHistory = true)
480541
}
481542
}
482543

544+
private bool CanAccessDirectory(string path)
545+
{
546+
try
547+
{
548+
Directory.GetDirectories(path);
549+
return true;
550+
}
551+
catch
552+
{
553+
return false;
554+
}
555+
}
556+
483557
private async Task LoadFilesAsync(string path)
484558
{
485559
var fileCollection = new ObservableCollection<FileItem>();
@@ -494,12 +568,10 @@ await Task.Run(async () =>
494568
//ディレクトリ
495569
foreach (var dir in Directory.EnumerateDirectories(path))
496570
{
571+
if (!CanAccessDirectory(dir)) continue;
497572
var info = new DirectoryInfo(dir);
498573

499-
if (
500-
!FileExplorerSettings.Default.ShowHiddenFiles
501-
&& info.Attributes.HasFlag(FileAttributes.Hidden)
502-
)
574+
if (!FileExplorerSettings.Default.ShowHiddenFiles && info.Attributes.HasFlag(FileAttributes.Hidden))
503575
continue;
504576

505577
var icon = await ShellIcon.GetIconAsync(dir, true);
@@ -554,6 +626,16 @@ await Application.Current.Dispatcher.InvokeAsync(() =>
554626
});
555627

556628
}
629+
catch (UnauthorizedAccessException)
630+
{
631+
fileCollection.Add(new FileItem
632+
{
633+
Name = "アクセス不可能なフォルダです",
634+
FullPath = path,
635+
Type = "",
636+
IsDirectory = true,
637+
});
638+
}
557639
catch (Exception ex)
558640
{
559641
Debug.WriteLine($"ファイルの読み込みに失敗: {ex.Message}");
@@ -1023,7 +1105,7 @@ private async void ShowLargePreview_Click(object sender, RoutedEventArgs e)
10231105
PreviewName.Text = selectedItem.Name;
10241106
PreviewColumn.Width = new GridLength(300);
10251107
LargePreviewSplitter.Visibility = Visibility.Visible;
1026-
LargePreviewPane.Visibility = Visibility.Visible;
1108+
LargePreviewPanel.Visibility = Visibility.Visible;
10271109

10281110
if (selectedItem.IsDirectory || string.IsNullOrEmpty(selectedItem.FullPath))
10291111
{
@@ -1233,7 +1315,7 @@ private void ClosePreview_Click(object sender, RoutedEventArgs e)
12331315

12341316
PreviewContentHost.Content = null;
12351317

1236-
LargePreviewPane.Visibility = Visibility.Collapsed;
1318+
LargePreviewPanel.Visibility = Visibility.Collapsed;
12371319
LargePreviewSplitter.Visibility = Visibility.Collapsed;
12381320
PreviewColumn.Width = new GridLength(0, GridUnitType.Auto);
12391321
}
@@ -1555,7 +1637,7 @@ private async Task NavigateHistory()
15551637
await SelectTreeViewItemByPathAsync(pathToNavigate);
15561638
}
15571639

1558-
if (Directory.Exists(pathToNavigate))
1640+
if (Directory.Exists(pathToNavigate) && CanAccessDirectory(pathToNavigate))
15591641
{
15601642
_currentDirectory = pathToNavigate;
15611643

YMM4FileExplorer/YMM4FileExplorer.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<Nullable>enable</Nullable>
77
<UseWPF>true</UseWPF>
88

9-
<Version>1.5.2</Version>
9+
<Version>1.5.3</Version>
1010

1111
<!--Pdb内にソースコードのフルパスを含めない-->
1212
<RepoRoot>$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)..\'))</RepoRoot>

0 commit comments

Comments
 (0)