Skip to content

Commit d41cf03

Browse files
Check if a path is a file or a directory: retarget net10.0, add Path.Exists, guard File.GetAttributes
- Both projects net7.0 to net10.0. - Tests: Microsoft.NET.Test.Sdk 17.7.1 to 18.10.1, MSTest.TestAdapter and MSTest.TestFramework 2.2.10 replaced by the MSTest 4.4.1 meta-package, coverlet.collector 3.2.0 to 10.0.1. Assert.ThrowsException renamed to Assert.ThrowsExactly at the one call site MSTest 4 breaks. - FileAttributesUsage: the try/catch that caught only FileNotFoundException becomes a Path.Exists() guard. File.GetAttributes() throws DirectoryNotFoundException when a parent directory is missing, which the old catch let through. - File.CreateText() replaced by File.WriteAllText(path, string.Empty) in seven files, so the StreamWriter is no longer left open. - FileInfoAndDirectoryInfoDrawbacks now calls Refresh() and prints the fresh answer after the stale one. - New PathExistsUsage demonstration and PathTypeWithPathExistsIntegrationTest. - Unused usings removed; three Run()-only classes made static.
1 parent ee4ab1b commit d41cf03

12 files changed

Lines changed: 106 additions & 31 deletions

‎files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net7.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>

‎files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/FileAndDirectoryUsage.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static void Run()
1111

1212
// file
1313
var testFile = Path.Combine(Path.GetTempPath(), "test_file1.abc");
14-
File.CreateText(testFile);
14+
File.WriteAllText(testFile, string.Empty);
1515

1616
isFile = File.Exists(testFile);
1717
isDirectory = Directory.Exists(testFile);

‎files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/FileAttributesUsage.cs‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace CheckPathFileOrDirectoryCSharp
22
{
3-
public class FileAttributesUsage
3+
public static class FileAttributesUsage
44
{
55
public static void Run()
66
{
@@ -11,7 +11,7 @@ public static void Run()
1111

1212
// file
1313
var testFile = Path.Combine(Path.GetTempPath(), "test_file4.abc");
14-
File.CreateText(testFile);
14+
File.WriteAllText(testFile, string.Empty);
1515

1616
var attributes = File.GetAttributes(testFile);
1717

@@ -32,18 +32,19 @@ public static void Run()
3232
Console.WriteLine($"{testDirectory}: isFile = {isFile}, isDirectory = {isDirectory}\n");
3333

3434
// no file or directory
35-
var notExistingPath = "someNotExistingPath4";
35+
var notExistingPath = Path.Combine(Path.GetTempPath(), "someNotExistingPath4");
3636

37-
try
37+
isDirectory = false;
38+
isFile = false;
39+
40+
if (Path.Exists(notExistingPath))
3841
{
3942
attributes = File.GetAttributes(notExistingPath);
43+
4044
isDirectory = attributes.HasFlag(FileAttributes.Directory);
4145
isFile = !isDirectory;
4246
}
43-
catch (FileNotFoundException)
44-
{
45-
isFile = isDirectory = false;
46-
}
47+
4748
Console.WriteLine($"{notExistingPath}: isFile = {isFile}, isDirectory = {isDirectory}\n");
4849
}
4950
}

‎files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/FileInfoAndDirectoryInfoDrawbacks.cs‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
7-
namespace CheckPathFileOrDirectoryCSharp
1+
namespace CheckPathFileOrDirectoryCSharp
82
{
9-
public class FileInfoAndDirectoryInfoDrawbacks
3+
public static class FileInfoAndDirectoryInfoDrawbacks
104
{
115
public static void Run()
126
{
@@ -23,13 +17,18 @@ public static void Run()
2317
Console.WriteLine($"existsFileInfo = {existsFileInfo}");
2418
Console.WriteLine($"existsFile = {existsFile}");
2519

26-
File.CreateText(testFile);
20+
File.WriteAllText(testFile, string.Empty);
2721

2822
existsFileInfo = fileInfo.Exists;
2923
existsFile = File.Exists(testFile);
3024

3125
Console.WriteLine($"existsFileInfo = {existsFileInfo}");
32-
Console.WriteLine($"existsFile = {existsFile}\n");
26+
Console.WriteLine($"existsFile = {existsFile}");
27+
28+
fileInfo.Refresh();
29+
existsFileInfo = fileInfo.Exists;
30+
31+
Console.WriteLine($"existsFileInfo after Refresh() = {existsFileInfo}\n");
3332
}
3433

3534
}

‎files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/FileInfoAndDirectoryInfoUsage.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace CheckPathFileOrDirectoryCSharp
22
{
3-
public class FileInfoAndDirectoryInfoUsage
3+
public static class FileInfoAndDirectoryInfoUsage
44
{
55
public static void Run()
66
{
@@ -14,7 +14,7 @@ public static void Run()
1414

1515
// file
1616
var testFile = Path.Combine(Path.GetTempPath(), "test_file2.abc");
17-
File.CreateText(testFile);
17+
File.WriteAllText(testFile, string.Empty);
1818

1919
fileInfo = new FileInfo(testFile);
2020
directoryInfo = new DirectoryInfo(testFile);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
namespace CheckPathFileOrDirectoryCSharp
2+
{
3+
public static class PathExistsUsage
4+
{
5+
public static void Run()
6+
{
7+
Console.WriteLine("Using Path.Exists:");
8+
9+
bool anythingHere;
10+
bool isDirectory;
11+
bool isFile;
12+
13+
// file
14+
var testFile = Path.Combine(Path.GetTempPath(), "test_file5.abc");
15+
File.WriteAllText(testFile, string.Empty);
16+
17+
anythingHere = Path.Exists(testFile);
18+
isDirectory = Directory.Exists(testFile);
19+
isFile = File.Exists(testFile);
20+
21+
Console.WriteLine($"{testFile}: anythingHere = {anythingHere}, isDirectory = {isDirectory}, isFile = {isFile}\n");
22+
23+
// directory
24+
var testDirectory = Path.Combine(Path.GetTempPath(), "test_directory5");
25+
Directory.CreateDirectory(testDirectory);
26+
27+
anythingHere = Path.Exists(testDirectory);
28+
isDirectory = Directory.Exists(testDirectory);
29+
isFile = File.Exists(testDirectory);
30+
31+
Console.WriteLine($"{testDirectory}: anythingHere = {anythingHere}, isDirectory = {isDirectory}, isFile = {isFile}\n");
32+
33+
// no file or directory
34+
var notExistingPath = Path.Combine(Path.GetTempPath(), "someNotExistingPath5");
35+
36+
anythingHere = Path.Exists(notExistingPath);
37+
isDirectory = Directory.Exists(notExistingPath);
38+
isFile = File.Exists(notExistingPath);
39+
40+
Console.WriteLine($"{notExistingPath}: anythingHere = {anythingHere}, isDirectory = {isDirectory}, isFile = {isFile}\n");
41+
}
42+
}
43+
}

‎files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/Program.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
FileAndDirectoryUsage.Run();
44
FileInfoAndDirectoryInfoUsage.Run();
55
FileInfoAndDirectoryInfoDrawbacks.Run();
6+
PathExistsUsage.Run();
67
FileAttributesUsage.Run();

‎files-csharp/CheckPathFileOrDirectoryCSharp/Tests/PathTypeWithAttributesIntegrationTest.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void WhenPathIsDirectory_ThenHasDirectoryAttribute()
1717
public void WhenPathIsFile_ThenHasNotDirectoryAttribute()
1818
{
1919
string path = Path.Combine(Path.GetTempPath(), "test_file_2.abc");
20-
File.CreateText(path);
20+
File.WriteAllText(path, string.Empty);
2121

2222
var attributes = File.GetAttributes(path);
2323
Assert.IsFalse(attributes.HasFlag(FileAttributes.Directory));
@@ -28,7 +28,7 @@ public void WhenPathNotExists_ThenThrowsException()
2828
{
2929
string path = Path.Combine(Path.GetTempPath(), "test_not_existing_2.abc");
3030

31-
Assert.ThrowsException<FileNotFoundException>(() => { var attributes = File.GetAttributes(path); });
31+
Assert.ThrowsExactly<FileNotFoundException>(() => { var attributes = File.GetAttributes(path); });
3232
}
3333
}
3434
}

‎files-csharp/CheckPathFileOrDirectoryCSharp/Tests/PathTypeWithFileAndDirectoryIntegrationTest.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void WhenPathIsDirectory_ThenDirectoryExistsIsTrue()
1717
public void WhenPathIsFile_ThenFileExistsIsTrue()
1818
{
1919
string path = Path.Combine(Path.GetTempPath(), "test_file_1.abc");
20-
File.CreateText(path);
20+
File.WriteAllText(path, string.Empty);
2121

2222
Assert.IsFalse(Directory.Exists(path));
2323
Assert.IsTrue(File.Exists(path));

‎files-csharp/CheckPathFileOrDirectoryCSharp/Tests/PathTypeWithFileInfoAndDirectoryInfoIntegrationTest.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void WhenPathIsDirectory_ThenDirectoryExistsIsTrue()
2020
public void WhenPathIsFile_ThenFileExistsIsTrue()
2121
{
2222
string path = Path.Combine(Path.GetTempPath(), "test_file_3.abc");
23-
File.CreateText(path);
23+
File.WriteAllText(path, string.Empty);
2424

2525
var fileInfo = new FileInfo(path);
2626
var directoryInfo = new DirectoryInfo(path);

0 commit comments

Comments
 (0)