disasterkid
disasterkid

Reputation: 7278

File.Exists returns false even though the file does exist

I'm building a console app under .NET Core (Version netcoreapp2.1) on macOS.

I check at the beginning of a method whether the file in question exists:

if (!File.Exists(filePath))
{
    Log.Error(string.Format("File not found: {0}", filePath));
    return null;
}

filePath contains the absolute path of the file e.g. "‎⁨/Users/myusername/Desktop/recipients.csv" and the file sits on my desktop. But when I debug, I see that the program does not see the file.

I have also tried the following string variations to no avail.

  1. "‎⁨//Users//myusername//Desktop//recipients.csv"
  2. @"‎⁨/Users/myusername/Desktop/recipients.csv"

This might be a very simple problem. But it has now taken about an hour.

Upvotes: 1

Views: 2772

Answers (1)

Cetin Basoz
Cetin Basoz

Reputation: 23797

(would be messy as a comment)

Try navigating to do folder in Terminal first. Like:

cd Desktop

Then use "pwd" command to see the path. On my system it is:

/Users/cetinbasoz/Desktop

I simply put a sample csv file named customer.csv there and ran this:

using System;
using System.IO;

namespace sandbox
{
    class Program
    {
        static void Main(string[] args)
        {
            var fileName = @"/Users/cetinBasoz/Desktop/customer.csv";
            if (File.Exists(fileName))
            {
                var content = File.ReadLines(fileName);
                foreach (var line in content)
                {
                    Console.WriteLine(line);
                }
                Console.WriteLine($"Dumped contents of {fileName}");
            }
        }
    }
}

And got this (shown partially):

"WOLZA","Wolski  Zajazd","Zbyszek Piestrzeniewicz","Owner","ul. Filtrowa 68","Warszawa","","01-012","Poland","(26) 642-7012","(26) 642-7012",3694.0000
"WINCA","Wenna Wines","Vladimir Yakovski","Owner","","","","","","","",0.0000
"XXXXXX","Linked Server Company","","","","","","","","","",0.0000
Dumped contents of /Users/cetinBasoz/Desktop/customer.csv

Press any key to continue...

Upvotes: 2

Related Questions