1

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.

disasterkid
  • 6,948
  • 25
  • 94
  • 179
  • Did you check casing? What if you put that file in Finder? What if you try `Directory.Exists`? When does it start returning false? – Patrick Hofman Dec 07 '18 at 13:07
  • @CaptainWibble: please don't add code tags on words. – Patrick Hofman Dec 07 '18 at 13:08
  • 1
    I think Users is actually somewhere else on your disk. Should be something like /Volumes/Mac/Users//Desktop/recipients.csv – sarvasana Dec 07 '18 at 13:10
  • Try using the full file path, including the drive letter. – Tom Johnson Dec 07 '18 at 13:11
  • 4
    @TomJohnson The box is running macOS and does not have drive letters but uses mounts. /Volumes/Mac/Users//Desktop/recipients.csv – sarvasana Dec 07 '18 at 13:12
  • Ah my bad, missed that bit. – Tom Johnson Dec 07 '18 at 13:13
  • Could be a permissions issue. Try File.OpenRead and see if that works – sr28 Dec 07 '18 at 13:19
  • This similar question might help: https://stackoverflow.com/questions/18266637/why-does-system-io-file-existsstring-path-return-false – sr28 Dec 07 '18 at 13:21
  • @Kris adding `/Volumes/Mac/` does not help. Plus this is the path Finder gives me and that's what I use as input. – disasterkid Dec 07 '18 at 13:35
  • @PatrickHofman `Directory.Exists` returns false, too. The path I found from Finder and I used that as input. – disasterkid Dec 07 '18 at 13:39
  • @PatrickHofman I tried `File.OpenRead(filePath)`. I suspect the debugger, instead of looking for the file using the absolute path, is appending the file path to project path. I get the following error: "An unhandled exception of type 'System.IO.DirectoryNotFoundException' occurred in System.Private.CoreLib.dll: 'Could not find a part of the path '/....PATH TO PROJECT FOLDER ...‎⁨/Users/myusername/Desktop/recipients.csv'.'" – disasterkid Dec 07 '18 at 13:47
  • Something is missing, post the code where `filePath`'s value is defined. – mxmissile Dec 07 '18 at 14:52
  • @Disasterkid Maybe your hard drive is probably not named Mac. I think the default used to be "Macintosh HD". This would make the correct path be /Volumes/Macintosh HD/Users//Desktop/recipients.csv Of course your drive could have any name. – sarvasana Dec 07 '18 at 21:59

1 Answers1

2

(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...
Cetin Basoz
  • 22,495
  • 3
  • 31
  • 39
  • I am sorry but I get the same result. File is not found. This is strange. – disasterkid Dec 07 '18 at 14:19
  • 2
    @Disasterkid, it is not the same result. I get a proper result as shown in the above sample. If it is saying 'file not found' then there is not such a file. Can you cat it in Terminal? And are you logged in as that user? – Cetin Basoz Dec 07 '18 at 14:47
  • There is only one user and that is me. The file has permission to me (read and write). staff (read only) and everyone (read only). What I meant by "file not found" was that `File.Exists()` returns false. – disasterkid Dec 07 '18 at 14:51
  • 1
    @Disasterkid, I believe you are not writing the path and filename right. – Cetin Basoz Dec 07 '18 at 14:52
  • @Disasterkid, why would I edit my answer? Desktop works just fine for me. I already told you to get it from the Terminal, no? And BTW, there is not such case sensitivity on filename, both Desktop and desktop works for me. – Cetin Basoz Dec 07 '18 at 15:04
  • @Disasterkid, As a side note, I wouldn't trust .Net core on Mac anyway. It has lots of libraries there as if they are working but not in fact. ie: Try Environment.SpecialFolder.Desktop. It thinks you are using .Net core on windows :) That is one of the reasons, despite using C#, switching to a better language like Go. – Cetin Basoz Dec 07 '18 at 15:08