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

Hi,

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)
        {
            i++;
            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

WebAppDeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
wht1986Commented:
try

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

This will return an array of strings
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
WebAppDeveloperAuthor Commented:
It works. Thanks very much!
0
 
wht1986Commented:
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 :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.