gitlabEdit

chart-mixedFiles - NKS0015

Use Moving.MoveFileOrDir()

This analyzer provides the following strings:

Context
String

Error List

Caller uses File.Move instead of FilesystemTools.MoveFileOrDir()

Suggestion Box

Use FilesystemTools.MoveFileOrDir() instead of File.Move

Description

FilesystemTools.MoveFileOrDir() neutralizes the provided path to its absolute correct path, while File.Move operates at the executable directory (Environment.CurrentDirectory), which may not be what you want.


Extended Description

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

Using File.Move(), 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 MoveFileOrDir(), 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

Somewhere in your mod code...
public static void MyFunction()
{
    File.Move("test.txt", PathsManagement.AppDataPath);
}

After the fix

Last updated