Code to optimize

Hi,

Can someone tell me if the attached code can be improved, shortened or optimized? If so, please make the necessary adjustments for me.

Thanks very much in advance.

using System;
using System.IO;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Xml;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
 
public class RandomImage
{
    public static string ImageUrl
    {
        get
        {
            HttpContext context = HttpContext.Current;
            string imageDir = "images/";
            string physicalPath = context.Server.MapPath("~/images");
            string[] filePath = Directory.GetFiles(physicalPath);
            //string[] filePath = Directory.GetFiles(physicalPath, "*.gif"); //Restrict to gif image files only
            DirectoryInfo dirInfo = new DirectoryInfo(physicalPath);
            FileInfo[] imageFiles = dirInfo.GetFiles();
            string listFiles = "";
            string imageType = "random_";
            int imageTypeLen = imageType.Length;
            int i = 0;
            foreach (FileInfo file in imageFiles)
            {
                string fileName = file.Name;
                if (fileName.Length >= imageTypeLen)
                {
                    string segment = fileName.Substring(0, imageTypeLen);
                    if (String.Compare(segment, imageType, true) == 0)
                    {
                        listFiles += imageDir + fileName + ",";
                        i++;
                    }
                }
            }
            if (i > 0)
                listFiles = listFiles.Substring(0, listFiles.Length - 1); //Knock off the last ","
            //string path = context.Request.ApplicationPath + GetRandomImage(listFiles);
            string path = GetRandomImage(listFiles);
            return path;
        }
    }
 
    private static string GetRandomImage(string files)
    {
        Random r = new Random();
        string[] images = files.Split(','); //Creates an array of strings
         return images[r.Next(0, images.Length)];
    }
}

Open in new window

WebAppDeveloperAsked:
Who is Participating?
 
williamcampbellConnect With a Mentor Commented:
There yah go .. i just left it out since it didnt change
   public class RandomImage
    {
        String _listFiles;
        private String ListFiles ()
        {
            if ( String.IsNullOrEmpty ( _listFiles ) )
            {
                HttpContext context = HttpContext.Current;
                string imageDir = "images/";
                string physicalPath = context.Server.MapPath("~/images");
                string[] filePath = Directory.GetFiles(physicalPath);
                //string[] filePath = Directory.GetFiles(physicalPath, "*.gif"); //Restrict to gif image files only
                DirectoryInfo dirInfo = new DirectoryInfo(physicalPath);
                FileInfo[] imageFiles = dirInfo.GetFiles();
                string imageType = "random_";
                int imageTypeLen = imageType.Length;
                int i = 0;
                foreach (FileInfo file in imageFiles)
                {
                    string fileName = file.Name;
                    if (fileName.Length >= imageTypeLen)
                    {
                        string segment = fileName.Substring(0, imageTypeLen);
                        if (String.Compare(segment, imageType, true) == 0)
                        {
                            listFiles += imageDir + fileName + ",";
                            i++;
                        }
                    }
                }
                if (i > 0)
                    _listFiles = listFiles.Substring(0, listFiles.Length - 1); //Knock off the last ","
            }
 
            return _listFiles;
        }
 
        public static string ImageUrl
        {
            get
            {
                string path = GetRandomImage(ListFiles);
 
                return path;
            }
        }
 
        private static string GetRandomImage(string files)
        {
            Random r = new Random();
            string[] images = files.Split(','); //Creates an array of strings
            return images[r.Next(0, images.Length)];
        }
    }

Open in new window

0
 
williamcampbellCommented:
Most the File List Creation out of the main Function and only call it one time

Also Use StringBuilder instead of String .. minor
        String _listFiles;
        private String ListFiles ()
        {
            if ( String.IsNullOrEmpty ( _listFiles ) )
            {
                HttpContext context = HttpContext.Current;
                string imageDir = "images/";
                string physicalPath = context.Server.MapPath("~/images");
                string[] filePath = Directory.GetFiles(physicalPath);
                //string[] filePath = Directory.GetFiles(physicalPath, "*.gif"); //Restrict to gif image files only
                DirectoryInfo dirInfo = new DirectoryInfo(physicalPath);
                FileInfo[] imageFiles = dirInfo.GetFiles();
                string imageType = "random_";
                int imageTypeLen = imageType.Length;
                int i = 0;
                foreach (FileInfo file in imageFiles)
                {
                    string fileName = file.Name;
                    if (fileName.Length >= imageTypeLen)
                    {
                        string segment = fileName.Substring(0, imageTypeLen);
                        if (String.Compare(segment, imageType, true) == 0)
                        {
                            listFiles += imageDir + fileName + ",";
                            i++;
                        }
                    }
                }
                if (i > 0)
                    _listFiles = listFiles.Substring(0, listFiles.Length - 1); //Knock off the last ","
            }
 
            return _listFiles;
        }
 
        public static string ImageUrl
        {
            get
            {
                string path = GetRandomImage(ListFiles);
 
                return path;
            }
        }

Open in new window

0
 
williamcampbellCommented:
string path = GetRandomImage(ListFiles);
should read
string path = GetRandomImage( ListFiles () );
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
WebAppDeveloperAuthor Commented:
Hi williamcampbell,

What happened to the Random() function, e.g.,  "Random r = new Random();"?  My purpose is to get a randomly selected image from a list (array) of images thru the use of the "Random r = new Random();".

Would you please revise the code to include such requirement!

Thanks very much.
0
 
WebAppDeveloperAuthor Commented:
Hi williamcampbell,

In your private function "private String ListFiles () {......}", you use "String" with the "S" being capitalized. What's the difference between using small "s" and the big "S", although the result turns out the same?

Thanks.
0
 
käµfm³d 👽Commented:
There is no difference. "string" is an alias for "String." The same is true for "int" and "Int32" and "bool" and "Boolean."
0
 
WebAppDeveloperAuthor Commented:
Great. Thanks.
0
 
williamcampbellCommented:
I prefer the Color of the String and Int16. I also program in C++ and like to distinguish the keywords .. light Blue C# dark blue C++
0
 
WebAppDeveloperAuthor Commented:
williamcampbell,

oh really? For myself, i'm used to the lower case "string".
0
 
WebAppDeveloperAuthor Commented:
Hi williamcampbell,

In VS 2008, I'm getting a red underline for "GetRandomImage(ListFiles)" in the line "string path = GetRandomImage(ListFiles)" by Intellisense indicating something is wrong, and when I roll my mouse over the red underline, it says "The best overloaded method for 'RandomImage.GetRandomImage(string) has some invalid arguments".

Thanks.
0
 
WebAppDeveloperAuthor Commented:
Hi williamcampbell,

Any suggestion I can fix it?

Thanks.
0
 
käµfm³d 👽Commented:
You've declared ListFiles to take no arguments, yet you are trying to pass a string. williamcampbell suggested earlier to use ListFiles().
0
 
käµfm³d 👽Commented:
Disregard, I read that wrong.
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.