Skip to content

Latest commit

 

History

History
49 lines (30 loc) · 2.38 KB

File metadata and controls

49 lines (30 loc) · 2.38 KB
description
Use Checking.FolderExists()

📉 Files - NKS0019

This analyzer provides the following strings:

ContextString
Error ListCaller uses Directory.Exists instead of Checking.FolderExists()
Suggestion BoxUse Checking.FolderExists() instead of Directory.Exists
DescriptionChecking.FolderExists() neutralizes the provided path to its absolute correct path, while Directory.Exists operates at the executable directory (Environment.CurrentDirectory), which may not be what you want.

Extended Description

This code analyzer detects the usage of Exists from the standard Directory class found in the System.IO namespace.

Using Directory.Exists(), path neutralization doesn't take place to ensure that we have the correct absolute path. This causes some of the filesystem operations to operate on files, which are located in the wrong place, and, therefore, errors throw about a target not being found.

A solution to this problem was made with FolderExists(), because it takes care of the absolute paths and tries to reduce this kind of error caused by passing relative directories to the arguments.

Analysis Comparison

To get a brief insight about how this analyzer works, compare the two code blocks shown to you below:

Before the fix

public static void MyFunction()
{
    Directory.Exists("test");
}

After the fix

public static void MyFunction()
{
    Checking.FolderExists("test");
}

Suppression

You can suppress this suggestion by including it in the appropriate place, whichever is convenient.

For more information about how to suppress any warning issued by the Nitrocid analyzer, visit the below page:

{% embed url="https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/suppress-warnings" %}

Recommendation

We recommend that every caller which use this function use the recommended abovementioned method.