0

I am looking at System.IO and wrote a small method to count the number of files in a directory. The problem I have is that when I debug this piece of code in windows, I can enumerate over the collection when I hover over the collection. So here is the code and the image in Visual Studio on Windows.

File Enumeration while debugging

Here is the code in question:

static void Main(string[] args)
{
    var dir = new DirectoryInfo(@"/home/user1/Downloads/Folder that 
                  has spaces in name");
    var files = dir.EnumerateFiles();
    var count = files.Count();
    Console.WriteLine($"Number of files in Videos Folder is {count}");
}

This is what I get when I run same lines of code on ubuntu:

Error while running code in ubuntu

Can someone help me understand what is going on here? Both folders have files in them and permissions is not a problem since I am working with a folder in my home directory (\home\user\Downloads\Videos). Folder names aren't a problem as I already checked.

Joseph Izang
  • 766
  • 1
  • 13
  • 43
  • Windows use backslash (\\) for file path whereas Linux use forward slash (/). I think that's likely the reason. – Sach Aug 12 '20 at 16:41
  • Sorry I made a mistake with the path. Will correct that now. That also isn't the reason. Checked it and spent a bunch of time reading about the directory separator. Sorry for the mistake – Joseph Izang Aug 12 '20 at 16:44
  • DirectoryInfo will fail if you do not have permission to read any file in the folder. Try a foreach(var d in dir) and then add an exception handler try/catch so you do not exit the for loop. – jdweng Aug 12 '20 at 16:49
  • and what does `ls /home/user1/Downloads/Videos` show? – CodeCaster Aug 12 '20 at 16:53
  • @jdweng already checked the issue of permissions before posting this, permissions are not an issue. files are in my profile folder so I have full permissions here. – Joseph Izang Aug 12 '20 at 22:25
  • @CodeCaster ls shows a bunch of folders with videos in them similar to the files shown in windows environment. – Joseph Izang Aug 12 '20 at 22:26
  • Then why are you getting "Enumeration yielded no results"? – jdweng Aug 12 '20 at 23:55
  • So I did some more experimentation with the code in question and find that when the directory name has spaces in it is when this happens. just did some tests to confirm this. Has anyone experienced this before? Trying to understand why this is – Joseph Izang Aug 13 '20 at 11:10

0 Answers0