static public List<int> usedRandomNumbers = new List<int>();
static Random random = new Random(DateTime.Now.Millisecond);
public string GetRandomFileFromGivenFolderPath()
{
if (FolderPath_RandomFiles.Text != " ")
{
// Get an array of all file paths in the specified folder
string[] files = Directory.GetFiles(FolderPath_RandomFiles.Text);
// If no files are found, return null
if (files.Length == 0)
{
UpdateRichTextBox("No Files found!");
return "null";
}
// Generate a random number between 0 and the number of files minus 1
int randomIndex = -1;
do
{
randomIndex = random.Next(0, files.Length);
} while (usedRandomNumbers.Contains(randomIndex));
usedRandomNumbers.Add(randomIndex);
// Return the file path at the random index
return files[randomIndex];
}
else
{
UpdateRichTextBox("Folder path for \"random Files Path\" not found!");
return "null";
}
}