Stephen S.
Stephen S.

Reputation: 3

How to load paths into ListBox and afterwards start the file per selected Index

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();


    }

    private void cmdAdd_Click(object sender, EventArgs e)
    {

        OpenFileDialog OP = new OpenFileDialog();
        OP.Title = "Please select the wanted .exe";
        string FileName = String.Empty;
        string PathName = String.Empty;
        OP.InitialDirectory = @"C:\Users\" + Environment.UserName.ToString() + @"\Desktop";
        OP.DefaultExt = ".exe";
        OP.Filter = "Game executable (*.exe) |*.exe";
        DialogResult Ergebnis = OP.ShowDialog();

        if (Ergebnis == DialogResult.OK)
        {
            FileInfo File = new FileInfo(OP.FileName);

            if (File.Exists)
            {
                PathName = File.FullName;
            }
        }


        if (PathName != String.Empty)
        {

            textBox1.Text = PathName;
            listBox1.Items.Add(PathName);
        }
    }

    private void cmdStart_Click(object sender, EventArgs e)
    {
        string SelectedItem = "";
        if (listBox1.SelectedItem != null)
        {
            SelectedItem = listBox1.SelectedItem.ToString();
            /*MessageBox.Show(SelectedItem);*/
        }

        Process Pro = new Process();
        Pro.StartInfo.FileName = SelectedItem;
        DialogResult Ergebnis2 = MessageBox.Show("Would you like to start the Game right now?", "Game start?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

        if (Ergebnis2.Equals(true))
        {
            try
            {
                Pro.Start();
            }
            catch (Exception)
            {
                MessageBox.Show("The Start of the Program was aborted!\r\nOr you didn't specify the right Path!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

    }

    private void cmdSave_Click(object sender, EventArgs e)
    {
        StreamWriter SaveFile = new StreamWriter(@"C:\Users\" + Environment.UserName.ToString() + @"\Desktop\savedgames.txt");
        foreach (var item in listBox1.Items)
        {
            SaveFile.WriteLine(item.ToString());
        }

        SaveFile.Close();
        MessageBox.Show("EZPZ");
    }

    private void cmdLoad_Click(object sender, EventArgs e)
    {
        StreamReader sr = new StreamReader(@"C:\Users\" + Environment.UserName.ToString() + @"\Desktop\savedgames.txt");
        string line = string.Empty;
        try
        {

            line = sr.ReadLine();

            while (line != null)
            {
                this.listBox1.Items.Add(line);

                line = sr.ReadLine();
            }

            sr.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }
        finally
        {
            sr.Close();
        }
    }
}

Hello Stackoverflow-Community,

So i've tried to be able to start the selected File(from the Listbox) by clicking the Start button.The items in the Listbox are loaded in from a .txt-File, But it seems that the path that i get (from the .txt-File) is not actually the same that was written inside.

For example: H:\Exe\556689600.exe is written inside the .txt-File but when i check while pausing the application the value of the SelectedItem is "H:(two backslashes)Exe(two backslashes)556689600.exe" so i'd like it to be H:\Exe\556689600.exe so it can be properly started.

EDIT: The main problem is that i can't start the .exe that i selected (via cmdStart) and i don't know why.

Please keep in mind that i'm (as you can see from the code) not very experienced in programming and that i'm not an native english speaker, so pardon me for any grammatical mistakes/logic mistakes made.

Thanks in advance, Stephen

Upvotes: 0

Views: 48

Answers (1)

Ori Nachum
Ori Nachum

Reputation: 598

The problem is with:

        DialogResult Ergebnis2 = MessageBox.Show("Would you like to start the Game right now?", "Game start?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        if (Ergebnis2.Equals(true))

DialogResult holds the Enum data 'DialogResult.Yes', so you need to compare it to that value, and not (true).

Edit:
I suggest practicing working with debug:
In this case, I plated a breakpoint on the 'cmdstart_Click' method and followed it step by step (Used F10)
I saw that we jump over the 'if' condition, and checked why.

Upvotes: 1

Related Questions