mercredi 30 septembre 2020

Randomly select 40 files from a folder which has 100 files [closed]

I'm trying to select certain amount of files randomly from a folder. For example if a folder has 100 files, I want to select 40 files randomly rather than the first 40.

            string sourceFolder = //unc path;
            var dir = new DirectoryInfo(sourceFolder );
            var allFiles = dir.GetFiles("*.pdf");
            int fileCount = allFiles.Length; // 100 files
            int folderOne = 60;
            int folderTwo = 40;

            if (fileCount > 0)
            {
                // select 60 files randomly and move them to folderOne
            }

I tried using Random function in C# but I cannot get my head around it.

             var random = new Random();
             int index = random.Next(0, fileCount - 1);
             var file = allFiles[index].FullName;

Any help would be greatly appreciated. Thanks




Aucun commentaire:

Enregistrer un commentaire