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 Listing.CreateList()

📉 Files - NKS0010

This analyzer provides the following strings:

ContextString
Error ListCaller uses Directory.GetFileSystemEntries instead of Listing.CreateList()
Suggestion BoxUse Listing.CreateList() instead of Directory.GetFileSystemEntries
DescriptionListing.CreateList() returns a list of FileSystemEntry instances that provides you information about a file, as well as a wrapper to the FileSystemInfo instance for that file.

Extended Description

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

The GetFileSystemEntries() from the Directory class gives you a randomly-sorted list of files and directories, which may not be convenient for TUI applications, such as the interactive TUI.

CreateList() takes care of that by sorting directories and files in an alphabetical order by putting the directories first before the files. This way, this listing can then be used for interactive applications.

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()
{
    var files = Directory.GetFileSystemEntries(PathsManagement.AppDataPath);
}

After the fix

public static void MyFunction()
{
    var files = Listing.CreateList(PathsManagement.AppDataPath);
}

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.