Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Commit 0250e70

Browse files
committed
add common access check method to utils
1 parent 7e3c299 commit 0250e70

File tree

3 files changed

+32
-21
lines changed

3 files changed

+32
-21
lines changed

UI/MainWindow/MainWindow.xaml.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,7 @@ public bool TryLoadSourceFile(string filePath, out EditorElement outEditor, bool
353353
fileInfo.Extension == ".ini")
354354
{
355355
var finalPath = fileInfo.FullName;
356-
try
357-
{
358-
File.GetAccessControl(finalPath);
359-
}
360-
catch (UnauthorizedAccessException)
356+
if (!DirUtils.CanAccess(finalPath))
361357
{
362358
return false;
363359
}

UI/MainWindow/MainWindowObjectBrowser.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -502,11 +502,7 @@ private void ChangeObjectBrowserToDirectory(string dir)
502502
{
503503
dir = Environment.CurrentDirectory;
504504
}
505-
try
506-
{
507-
Directory.GetAccessControl(dir);
508-
}
509-
catch (UnauthorizedAccessException)
505+
if (!DirUtils.CanAccess(dir))
510506
{
511507
return;
512508
}
@@ -613,15 +609,7 @@ private List<TreeViewItem> BuildDirectoryItems(string dir, out List<TreeViewItem
613609
if (attr.HasFlag(FileAttributes.Directory))
614610
{
615611
var dInfo = new DirectoryInfo(item);
616-
if (!dInfo.Exists)
617-
{
618-
continue;
619-
}
620-
try
621-
{
622-
dInfo.GetAccessControl();
623-
}
624-
catch (UnauthorizedAccessException)
612+
if (!dInfo.Exists || !DirUtils.CanAccess(dInfo))
625613
{
626614
continue;
627615
}

Utils/DirUtils.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
using System.IO;
1+
using System;
2+
using System.IO;
23

34
namespace SPCode.Utils
45
{
56
public static class DirUtils
67
{
7-
public static void ClearTempFolder()
8+
public static void ClearSPCodeTempFolder()
89
{
910
var tempDir = PathsHelper.TempDirectory;
1011
foreach (var file in Directory.GetFiles(tempDir))
@@ -16,5 +17,31 @@ public static void ClearTempFolder()
1617
Directory.Delete(dir, true);
1718
}
1819
}
20+
21+
public static bool CanAccess(string path)
22+
{
23+
try
24+
{
25+
Directory.GetAccessControl(path);
26+
}
27+
catch (UnauthorizedAccessException)
28+
{
29+
return false;
30+
}
31+
return true;
32+
}
33+
34+
public static bool CanAccess(DirectoryInfo dir)
35+
{
36+
try
37+
{
38+
dir.GetAccessControl();
39+
}
40+
catch (UnauthorizedAccessException)
41+
{
42+
return false;
43+
}
44+
return true;
45+
}
1946
}
2047
}

0 commit comments

Comments
 (0)