-1
string fileName = args[1];

Now for example i have in fileName e:\test1\myimage.bmp

Now i want to get all the files in test1. I want to extract the directory test1 from fileName and then to get all the files names from test1.

And the get files should get specific files types only images types for example *.bmp or *.jpeg or *.tiff and not to get all the files types in the directory.

Rubi Reubi
  • 103
  • 8
  • 2
    I suggest you to look at the Path and Directory classes – Steve Feb 06 '16 at 23:16
  • 1
    Possible duplicate of [How to recursively list all the files in a directory in C#?](http://stackoverflow.com/questions/929276/how-to-recursively-list-all-the-files-in-a-directory-in-c) and [Getting all file names from a folder using C#](http://stackoverflow.com/questions/14877237/getting-all-file-names-from-a-folder-using-c-sharp) – C-Pound Guru Feb 06 '16 at 23:21

3 Answers3

3

To get a parent folder of a specifique file just use this line Path.GetDirectoryName(fileName);

You can get all files that match bmp, jpeg or tiff extensions just use the following code Directory.EnumerateFiles. Finally you must have something like this code ;

var directory = System.IO.Path.GetDirectoryName(fileName);
var files = System.IO.Directory.EnumerateFiles(directory, "*.*")
    .Where(s => s.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase)
            || s.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase)
            || s.EndsWith(".tiff ", StringComparison.OrdinalIgnoreCase));
CodeNotFound
  • 22,153
  • 10
  • 68
  • 69
1

First of all you need the parent directory so you can use System.IO.Directory.GetParent(). Once you have get the parent directory use Directory.GetFiles(path, extension) to get all the files with that extensions.

Tinwor
  • 7,765
  • 6
  • 35
  • 56
1

For this you'll want to use System.IO namespace.

void test()
{
    var fileName = @"c:\a\a.php";
    var dir = Directory.GetParent(fileName);
    string[] files = Directory.GetFiles(dir);
}

Using the Directory class you're able to get list of files as an array of strings.

This, however wouldn't limit the files to images only but you can use simple LINQ to fix it:

void test()
{
    var fileName = @"c:\a\a.php";
    var dir = Directory.GetParent(fileName);
    string[] files = Directory.GetFiles(dir).Where(o => o.EndsWith(".bmp") | o.EndsWith(".jpg") | o.EndsWith(".png")).ToArray();
}

This Requires System.Linq namespace.

Reynevan
  • 1,475
  • 1
  • 18
  • 35