Given a directory select a random file from it.
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace Dropboxifier
{
static public class FileUtils
{
/// <summary>
/// Given a directory select a random file from it.
/// </summary>
static public string SelectRandomFileFromFolder(string folderName)
{
DirectoryInfo di = new DirectoryInfo(folderName);
if (di.Exists)
{
FileInfo[] files = di.GetFiles();
if (files.Length > 0)
{
Random randGenerator = new Random();
int index = randGenerator.Next(0, files.Length - 1);
return files[index].FullName;
}
}
return null;
}
}
}
Related examples in the same category