How to create a list of file names from a folder in C #

I am having a problem when I want to populate an array with file names.

the code:

 string [] arrays= {};
 String sdira= "path of the directory";

 foreach (string d in Directory.GetDirectories(sdira))
 {

   foreach (string f in Directory.GetFiles(d, "*.*"))`enter code here`
   {
     int j = 0;

     array[j++] = path.getfiles(f);
   }

 }

when I loop through the array, the array does not contain anything :( so how to fill the array in this case? or any better solution :)

+3
source share
6 answers

Solution sent to your other question . This eliminates the need for loops at all

string [] arrays;
String sdira= "path of the directory";

arrays =  Directory.GetFiles(sdira, "*", SearchOption.AllDirectories).Select(x => Path.GetFileName(x)).ToArray();

can also perform

arrays = Directory.GetFiles(sdira, "*", SearchOption.AllDirectories)
    .Where(s => (Path.GetExtension(s).ToLower() == extensionType)).ToArray();

if you want to get only the answer .jpg (as indicated in your other question) extensionType = ".jpg"

+5
source

you return j to 0 at the beginning of each iteration of the loop.

How about this:

List<string> files = new List<string>();
String sdira = "path of the directory";

foreach (string d in Directory.GetDirectories(sdira)) {

    foreach (string f in Directory.GetFiles(d, "*.*")) {
       files.Add(f);
    }
}
+6
source

.

string[] array = new string[16]; // this will create an arrray of length 16. 

.

, .

+2

List<string>. , , .

var files = new List<string>(); 
string sdira= "path of the directory"; 
foreach (string d in Directory.GetDirectories(sdira)) {   
    files.AddRange(Directory.GetFiles(d, "*.*"));   
}

// To use
foreach(string file in files) { }
string file2 = files[2]; 
// or convert to array
string[] arrays = files.ToArray();

// Or to use as datasource    
comboBox1.DataSource = files;
+1

, , , , , List<T>, i.e.

 List<string> Filenames = List<string>();
 String sdira= "path of the directory";

 foreach (string d in Directory.GetDirectories(sdira))
 {
   foreach (string f in Directory.GetFiles(d, "*.*"))
   {
       Filenames.Add(f);
   }
 }
+1

, arrays, . , . .

, System.Collections.Generic.List , , ( , arrays. :

System.Collections.Generic.List<string> filesList = new System.Collections.Generic.List<string>();
string path = @"c:\my\directory\";

foreach (string directory in System.IO.Directory.GetDirectories(path))
{
    foreach (string file in System.IO.Directory.GetFiles(directory))
    {
        filesList.Add(file);
    }
}

string[] filesArray = filesList.ToArray();

EDIT: , :

System.Collections.Generic.List<string> filesList = new System.Collections.Generic.List<string>();
string path = @"c:\my\directory\";

if (System.IO.Directory.Exists(path))
{
    //Get files for the base path
    string[] baseDirectoryFiles = System.IO.Directory.GetFiles(path);
    filesList.AddRange(baseDirectoryFiles);

    // Get files in subdirectories (first level) of base path
    foreach (string directory in System.IO.Directory.GetDirectories(path))
    {
        string[] directoryFiles = System.IO.Directory.GetFiles(directory);
        filesList.AddRange(directoryFiles);
    }

    string[] filesArray = filesList.ToArray();
}
else
{
    //Your directory was not found on the filesystem
    //handle as appropriate
}

, . , .

, , , , , :

private void MyMethod()
{
    List<string> fileNames = new List<string>();
    GetFileNames(@"C:\my\base\directory", fileNames);
}

private void GetFileNames(string directory, List<string> files)
{
    files.AddRange(System.IO.Directory.GetFiles(directory));

    foreach(string subDirectory in System.IO.Directory.GetDirectories(directory))
    {
        GetFileNames(subDirectory, files);
    }
}
+1
source

Source: https://habr.com/ru/post/1787719/


All Articles