Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static void Run()

// file
var testFile = Path.Combine(Path.GetTempPath(), "test_file1.abc");
File.CreateText(testFile);
File.WriteAllText(testFile, string.Empty);

isFile = File.Exists(testFile);
isDirectory = Directory.Exists(testFile);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace CheckPathFileOrDirectoryCSharp
{
public class FileAttributesUsage
public static class FileAttributesUsage
{
public static void Run()
{
Expand All @@ -11,7 +11,7 @@ public static void Run()

// file
var testFile = Path.Combine(Path.GetTempPath(), "test_file4.abc");
File.CreateText(testFile);
File.WriteAllText(testFile, string.Empty);

var attributes = File.GetAttributes(testFile);

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

// no file or directory
var notExistingPath = "someNotExistingPath4";
var notExistingPath = Path.Combine(Path.GetTempPath(), "someNotExistingPath4");

try
isDirectory = false;
isFile = false;

if (Path.Exists(notExistingPath))
{
attributes = File.GetAttributes(notExistingPath);

isDirectory = attributes.HasFlag(FileAttributes.Directory);
isFile = !isDirectory;
}
catch (FileNotFoundException)
{
isFile = isDirectory = false;
}

Console.WriteLine($"{notExistingPath}: isFile = {isFile}, isDirectory = {isDirectory}\n");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CheckPathFileOrDirectoryCSharp
namespace CheckPathFileOrDirectoryCSharp
{
public class FileInfoAndDirectoryInfoDrawbacks
public static class FileInfoAndDirectoryInfoDrawbacks
{
public static void Run()
{
Expand All @@ -23,13 +17,18 @@ public static void Run()
Console.WriteLine($"existsFileInfo = {existsFileInfo}");
Console.WriteLine($"existsFile = {existsFile}");

File.CreateText(testFile);
File.WriteAllText(testFile, string.Empty);

existsFileInfo = fileInfo.Exists;
existsFile = File.Exists(testFile);

Console.WriteLine($"existsFileInfo = {existsFileInfo}");
Console.WriteLine($"existsFile = {existsFile}\n");
Console.WriteLine($"existsFile = {existsFile}");

fileInfo.Refresh();
existsFileInfo = fileInfo.Exists;

Console.WriteLine($"existsFileInfo after Refresh() = {existsFileInfo}\n");
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace CheckPathFileOrDirectoryCSharp
{
public class FileInfoAndDirectoryInfoUsage
public static class FileInfoAndDirectoryInfoUsage
{
public static void Run()
{
Expand All @@ -14,7 +14,7 @@ public static void Run()

// file
var testFile = Path.Combine(Path.GetTempPath(), "test_file2.abc");
File.CreateText(testFile);
File.WriteAllText(testFile, string.Empty);

fileInfo = new FileInfo(testFile);
directoryInfo = new DirectoryInfo(testFile);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace CheckPathFileOrDirectoryCSharp
{
public static class PathExistsUsage
{
public static void Run()
{
Console.WriteLine("Using Path.Exists:");

bool anythingHere;
bool isDirectory;
bool isFile;

// file
var testFile = Path.Combine(Path.GetTempPath(), "test_file5.abc");
File.WriteAllText(testFile, string.Empty);

anythingHere = Path.Exists(testFile);
isDirectory = Directory.Exists(testFile);
isFile = File.Exists(testFile);

Console.WriteLine($"{testFile}: anythingHere = {anythingHere}, isDirectory = {isDirectory}, isFile = {isFile}\n");

// directory
var testDirectory = Path.Combine(Path.GetTempPath(), "test_directory5");
Directory.CreateDirectory(testDirectory);

anythingHere = Path.Exists(testDirectory);
isDirectory = Directory.Exists(testDirectory);
isFile = File.Exists(testDirectory);

Console.WriteLine($"{testDirectory}: anythingHere = {anythingHere}, isDirectory = {isDirectory}, isFile = {isFile}\n");

// no file or directory
var notExistingPath = Path.Combine(Path.GetTempPath(), "someNotExistingPath5");

anythingHere = Path.Exists(notExistingPath);
isDirectory = Directory.Exists(notExistingPath);
isFile = File.Exists(notExistingPath);

Console.WriteLine($"{notExistingPath}: anythingHere = {anythingHere}, isDirectory = {isDirectory}, isFile = {isFile}\n");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
FileAndDirectoryUsage.Run();
FileInfoAndDirectoryInfoUsage.Run();
FileInfoAndDirectoryInfoDrawbacks.Run();
PathExistsUsage.Run();
FileAttributesUsage.Run();
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void WhenPathIsDirectory_ThenHasDirectoryAttribute()
public void WhenPathIsFile_ThenHasNotDirectoryAttribute()
{
string path = Path.Combine(Path.GetTempPath(), "test_file_2.abc");
File.CreateText(path);
File.WriteAllText(path, string.Empty);

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

Assert.ThrowsException<FileNotFoundException>(() => { var attributes = File.GetAttributes(path); });
Assert.ThrowsExactly<FileNotFoundException>(() => { var attributes = File.GetAttributes(path); });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void WhenPathIsDirectory_ThenDirectoryExistsIsTrue()
public void WhenPathIsFile_ThenFileExistsIsTrue()
{
string path = Path.Combine(Path.GetTempPath(), "test_file_1.abc");
File.CreateText(path);
File.WriteAllText(path, string.Empty);

Assert.IsFalse(Directory.Exists(path));
Assert.IsTrue(File.Exists(path));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void WhenPathIsDirectory_ThenDirectoryExistsIsTrue()
public void WhenPathIsFile_ThenFileExistsIsTrue()
{
string path = Path.Combine(Path.GetTempPath(), "test_file_3.abc");
File.CreateText(path);
File.WriteAllText(path, string.Empty);

var fileInfo = new FileInfo(path);
var directoryInfo = new DirectoryInfo(path);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace Tests
{
[TestClass]
public class PathTypeWithPathExistsIntegrationTest
{
[TestMethod]
public void WhenPathIsDirectory_ThenPathExistsIsTrue()
{
string path = Path.Combine(Path.GetTempPath(), "test_directory_4");
Directory.CreateDirectory(path);

Assert.IsTrue(Path.Exists(path));
}

[TestMethod]
public void WhenPathIsFile_ThenPathExistsIsTrue()
{
string path = Path.Combine(Path.GetTempPath(), "test_file_4.abc");
File.WriteAllText(path, string.Empty);

Assert.IsTrue(Path.Exists(path));
}

[TestMethod]
public void WhenPathNotExists_ThenPathExistsIsFalse()
{
string path = Path.Combine(Path.GetTempPath(), "test_not_existing_4.abc");

Assert.IsFalse(Path.Exists(path));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand All @@ -10,10 +10,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="coverlet.collector" Version="3.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.10.1" />
<PackageReference Include="MSTest" Version="4.4.1" />
<PackageReference Include="coverlet.collector" Version="10.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading