I am trying to list all the files in the Documents folder using an absolute path in C# using this C:\\Users\\USER\\Documents
, I check first if the path exists and then try to list all the files in that location but my conditional structure is returning false while the path exists on my machine, I guess the double slash is for escaping the single slash characters. Why is my code returning false for the File.Exists(path)
and the path does exist on my machine as C:\Users\USER\Documents
public class Solution
{
private static string[] files;
static void Main(string[] args)
{
string path = "C:\\Users\\USER\\Documents";
//check if the path exists
if (File.Exists(path))
{
//list all the files in the path
files = Directory.GetFiles(path);
//check if the count is greater than 0
if (files.Length > 0)
{
foreach (string r in files)
{
Console.WriteLine(r);
};
}
else
{
Console.WriteLine("This file location is empty");
}
}
else
{
//this section executes so the path is returning false
Console.WriteLine("Path does not exist");
}
}
}