Unicorn with delicious cookie
Nous utilisons des cookies pour améliorer votre expérience de navigation. En savoir plus
Accepter
to the top
>
>
>
V3039. Consider inspecting the 'Foo'...
menu mobile close menu
Additional information
toggle menu Contents

V3039. Consider inspecting the 'Foo' function call. Defining an absolute path to the file or directory is considered a poor style.

14 Déc 2015

The analyzer has detected a possible error in a call to a function intended for file handling. The error has to do with an absolute path to a file or directory being passed to the function as one of the arguments. Passing absolute paths as arguments can be dangerous since such paths may not exist on the user's computer.

Consider the following example:

String[] file = File.ReadAllLines(
  @"C:\Program Files\MyProgram\file.txt");

A better solution is to get the path to the file based on certain conditions.

This is what the fixed version of the code should look like:

String appPath = Path.GetDirectoryName(
                 Assembly.GetExecutingAssembly().Location);
String[] fileContent = File.ReadAllLines(
                       Path.Combine(appPath, "file.txt"));

In this code, the file will be looked up in the application's directory.

This diagnostic is classified as: