4.51 How to display random images on a web page from a set of images in a directory?
C#
System.IO.DirectoryInfo dirInfo =new System.IO.DirectoryInfo(@"c:\inetpub\wwwroot\SyncnewCsharp\images") ;//This is your path to the bmp's
System.IO.FileInfo[] fileInfo ;
string strRandomImage;
int i ;
Random rnd ;
fileInfo = dirInfo.GetFiles("*"); //Gets an array of file info
rnd = new Random();
Microsoft.VisualBasic.VBMath.Randomize ();
i = Convert.ToInt32 (rnd.NextDouble() * fileInfo.GetUpperBound(0));// gets a random index
strRandomImage = "Images/" + fileInfo[i].Name ;// Returns the random string
TD1.Attributes.Add ("background", strRandomImage);
source