📉Files - NKS0022

Use Parsing.GetInvalidPathChars()

This analyzer provides the following strings:

ContextString

Error List

Caller uses Path.GetInvalidPathChars instead of Parsing.GetInvalidPathChars()

Suggestion Box

Use Parsing.GetInvalidPathChars() instead of Path.GetInvalidPathChars

Description

Parsing.GetInvalidPathChars() always returns invalid characters for Windows paths, regardless of the host operating system, while Path.GetInvalidPathChars returns a list of forbidden path characters for an operating system, which may be wrong in .NET 6.0 or later for the following characters: '"', '<', '>'.

Extended Description

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

Using Path.GetInvalidPathChars(), a weirdness has been discovered on Windows systems running .NET 6.0 or later, because that function doesn't consider the three characters: ", <, and > illegal. Therefore, operations can be made to the files or folders on Windows systems with the three characters on them, causing undefined behavior.

A solution to this problem was made with GetInvalidPathChars() from Parsing, because it takes care of this pitfall on Windows systems by placing the three characters above to the blacklist.

Analysis Comparison

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

Before the fix

Somewhere in your mod code...
public static void MyFunction()
{
    var invalidChars = Path.GetInvalidPathChars();
}

After the fix

Somewhere in your mod code...
public static void MyFunction()
{
    var invalidChars = Parsing.GetInvalidPathChars();
}

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:

Recommendation

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

Last updated