Do you guys know a better way to write this?

bool a = Directory.Exists("C:\\Program Files\\Java\\jdk1.6.0_1");
        bool b = Directory.Exists("C:\\Program Files\\Java\\jdk1.6.0_2");
        bool c = Directory.Exists("C:\\Program Files\\Java\\jdk1.6.0_3");
        bool d = Directory.Exists("C:\\Program Files\\Java\\jdk1.6.0_4");
        bool ee = Directory.Exists("C:\\Program Files\\Java\\jdk1.6.0_5");
        bool f = Directory.Exists("C:\\Program Files\\Java\\jdk1.6.0_6");
        bool g = Directory.Exists("C:\\Program Files\\Java\\jdk1.6.0_7");
        bool h = Directory.Exists("C:\\Program Files\\Java\\jdk1.6.0_8");
        bool i = Directory.Exists("C:\\Program Files\\Java\\jdk1.6.0_9");
        bool j = Directory.Exists("C:\\Program Files\\Java\\jdk1.6.0_10");
        bool k = Directory.Exists("C:\\Program Files\\Java\\jdk1.6.0_11");
        bool l = Directory.Exists("C:\\Program Files\\Java\\jdk1.6.0_12");
        bool m = Directory.Exists("C:\\Program Files\\Java\\jdk1.6.0_13");
        bool n = Directory.Exists("C:\\Program Files\\Java\\jdk1.6.0_14");
        bool o = Directory.Exists("C:\\Program Files\\Java\\jdk1.6.0_15");
        bool p = Directory.Exists("C:\\Program Files\\Java\\jdk1.6.0_16");
        bool q = Directory.Exists("C:\\Program Files\\Java\\jdk1.6.0_17");
if (a)
            {
                label1.Text = "Jdk version: 1.6.0_1";
            }
            if (b)
            {
                label1.Text = "Jdk version: 1.6.0_2";
            }
            if (c)
            {
                label1.Text = "Jdk version: 1.6.0_3";
            }
            if (d)
            {
                label1.Text = "Jdk version: 1.6.0_4";
            }
            if (ee)
            {
                label1.Text = "Jdk version: 1.6.0_5";
            }
            if (f)
            {
                label1.Text = "Jdk version: 1.6.0_6";
            }
            if (g)
            {
                label1.Text = "Jdk version: 1.6.0_7";
            }
            if (h)
            {
                label1.Text = "Jdk version: 1.6.0_8";
            }
            if (i)
            {
                label1.Text = "Jdk version: 1.6.0_9";
            }
            if (j)
            {
                label1.Text = "Jdk version: 1.6.0_10";
            }
            if (k)
            {
                label1.Text = "Jdk version: 1.6.0_11";
            }
            if (l)
            {
                label1.Text = "Jdk version: 1.6.0_12";
            }
            if (m)
            {
                label1.Text = "Jdk version: 1.6.0_13";
            }
            if (n)
            {
                label1.Text = "Jdk version: 1.6.0_14";
            }
            if (o)
            {
                label1.Text = "Jdk version: 1.6.0_15";
            }
            if (p)
            {
                label1.Text = "Jdk version: 1.6.0_16";
            }
            if (q)
            {
                label1.Text = "Jdk version: 1.6.0_17";
            }

I know it can be done better, but I don't know how.

Construct a string with string s; s = "C:\\Program Files\\Java\\jdk1.6.0_"+num; . Then if (Directory.Exists(s)) is true, then output the substring of s that has "jdk"+ a string with "version" + the other stubstring "1.6.0_1".

Construct a string with string s; s = "C:\\Program Files\\Java\\jdk1.6.0_"+num; . Then if (Directory.Exists(s)) is true, then output the substring of s that has "jdk"+ a string with "version" + the other stubstring "1.6.0_1".

I think I'm doing something wrong cause that didn't work.

Edit: I used an array, and it worked.

I searched my registry and found the following key, which returned 1.6.0_15 on my machine:

RegistryKey rk = Registry.LocalMachine;
            RegistryKey subKey = rk.OpenSubKey("SOFTWARE\\JavaSoft\\Java Update\\Policy");
            string currentVerion = subKey.GetValue("InstalledJREVersion").ToString();

EDIT: If using the above, just concatenate the currentVersion to the "Jdk version: " text: Text = "Jdk version: " + currentVersion;

You might try something like

string() foldernames = System.IO.Directory.GetFiles("C:\\Program Files\\Java\\jdk*.*");
if (foldernames.Length > 0)
label1.Text = foldernames[0];

(it might be " string foldernames() = ... ", i forget string array declarations in C# right now :P)

List<string> Directories = new List<string>(System.IO.Directory.GetDirectories("C:\\"));
List<string> Files = new List<string>(System.IO.Directory.GetFiles("C:\\"));

Not sure if this helps but I was writing some code for a TreeView and created list<>, one for the directories and one for the files.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.