1

I need to copy files and directories within a directory. Suppose there is only one level.

There is a directory C:\AAAA which contains 5 other directories (sub-directories not having other children) and 7 files.

When I call:

System.IO.Directory.GetFileSystemEntries("C:\\AAAA");

which returns an string array. It will contain the entries sorted in dictonary sort order. So when I call a for-each loop:

foreach(string path in System.IO.Directory.GetFileSystemEntries("C:\\AAAA"))
{
    //how to determine path is dictonay or file??
}

so from the array returned by System.IO.Directory.GetFileSystemEntries("C:\AAAA"), how to determine that given path is a path to file or it is a directory?

Priyank Thakkar
  • 4,752
  • 19
  • 57
  • 93

7 Answers7

2

Check using Directory.Exists or File.Exists

Romil Kumar Jain
  • 20,239
  • 9
  • 63
  • 92
2

You can try Directory.Exists:

bool isDir = Directory.Exists(path)

or FileSystemInfo.Attributes:

var info = new FileInfo(path);
bool isDir = (info.Attributes & FileAttributes.Directory) == FileAttributes.Directory;

File.GetAttributes gets you those Attributes, too.

Botz3000
  • 39,020
  • 8
  • 103
  • 127
2

You can either use Directory.Exists / File.Exists or just do

Directory.GetFiles();
Directory.GetDirectories();

MSDN:

nxu
  • 2,202
  • 1
  • 22
  • 34
2

You could discover if a string represent a file or a directory using FileAttributes.

   foreach(string currentName in Directory.GetFileSystemEntries("C:\\AAAA")) 
   { 
       FileAttributes att = File.GetAttributes(currentName);
       if((att & FileAttributes.Directory) == FileAttributes.Directory)
           // is a directory
       else
           // is a file
   }

Using FileAttributes has only one disadvantage against File.Exist or Directory.Exist.
If the file or directory doesn't exist, it will throw an exception, but based on your code above, there are little possibilities to incur in this situation and File.GetAttributes is very fast compared.

Steve
  • 213,761
  • 22
  • 232
  • 286
1
foreach(string path in System.IO.Directory.GetFileSystemEntries("C:\\AAAA"))
{
    // get the file attributes for file or directory
    FileAttributes attr = File.GetAttributes(path);

    //detect whether its a directory or file
    if((attr & FileAttributes.Directory) == FileAttributes.Directory)
        MessageBox.Show("Its a directory");
    else
        MessageBox.Show("Its a file");
}
Christophe Geers
  • 8,564
  • 3
  • 37
  • 53
MSUH
  • 872
  • 2
  • 9
  • 20
0

This ought to do it:

System.IO.Directory.GetFileSystemEntries("C:\\Temp");

foreach (string path in System.IO.Directory.GetFileSystemEntries("C:\\Temp"))
{
      if (File.Exists(path))
      {
          Console.WriteLine("im a file");
      }
      else
      {
          Console.WriteLine("im a folder");
       }
 }
dtsg
  • 4,408
  • 4
  • 30
  • 40
0

If you are needing to treat files and diretories differently I'd suggest getting them separately using GetDirectories and GetFiles and processing them as appropriate. That way you know what they are ahead of time.

I assume this will be better for what you are describing since you can loop through all the files and copy them and then loop through all the directories and call the copy recursively.

Yeah, I know this doesn't actually answer the question asked but I think it answers the spirit of how to do X. :)

Chris
  • 27,210
  • 6
  • 71
  • 92