Displaying random image from an array of images in ASP.NET using C#...

Posted on 2009-04-15
Last Modified: 2012-05-06

I have the following code that I'm trying to display an image randomly selected from a list of images in an array, but it doesn't seem to work.  Please help!

Very much appreciated.
<asp:Image ID="imgRandom" runat="server" Height="50px" Width="50px" />

    protected void Page_Load(object sender, EventArgs e)


        string imageDir = "images_random/";

        string physicalPath = Server.MapPath("/images_random");

        string[] filePath = Directory.GetFiles(physicalPath, "*.gif"); // i know all my images are gifs

        DirectoryInfo dirInfo = new DirectoryInfo(physicalPath);

        FileInfo[] imageFiles = dirInfo.GetFiles();

        string listFiles = "";

        int i = 0;

        foreach (FileInfo file in imageFiles)



            listFiles += imageDir + file.Name;

            if (i < imageFiles.Length)

                listFiles += ",";


        string path = Request.ApplicationPath + GetRandomImage(listFiles);

        imgRandom.ImageUrl = path; // display the random image


    protected string GetRandomImage(string files)


        Random r = new Random();

        string[] images = new string[] { files }; // this "files" variable should spit out the same list of images as those in the next commented line. If I comment out this line, and uncomment the next line, it works.

        //string[] images = new string[] { "images_random/someimage1002007.gif", "images_random/someimage-2008.gif", "images_random/someimage-2007.gif", "images_random/someimage-1987.gif", "images_random/someimage-1995.gif", "images_random/someimage-2001.gif", "images_random/someimage-2004.gif", "images_random/someimage-2006.gif" };

        return images[r.Next(0, images.Length)]; // this line works


Open in new window

Question by:WebAppDeveloper
    LVL 12

    Accepted Solution


    string[] images  = files.Split(',');

    This will return an array of strings

    Author Comment

    It works. Thanks very much!
    LVL 12

    Expert Comment

    Glad to be of service. If you could mark my response as the answer that would be awesome. Im racing a friend tonight for points :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now