2

I have added all startup programs to a listbox. How can I open the file when I select the item and click on a button?

Listbox code:

private void readfiles()
{
    string startfolder = Environment.GetFolderPath(Environment.SpecialFolder.Startup);

    var files = Directory.GetFiles(startfolder).Where(name => !name.EndsWith(".ini"));

    foreach (string file in files)
    {
        startupinfo.Items.Add(System.IO.Path.GetFileName(file));
    }
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
Sneakybastardd
  • 288
  • 1
  • 5
  • 17
  • This might help you [c# open file with default application and parameters][1] [1]: http://stackoverflow.com/questions/11365984/c-sharp-open-file-with-default-application-and-parameters – santosh shrestha Apr 10 '13 at 23:37
  • You are storing the filename only: GetFileName strips out the path. http://msdn.microsoft.com/en-us/library/system.io.path.getfilename.aspx – Blorgbeard Apr 11 '13 at 00:04

4 Answers4

2

I don't know what file type you're trying to open, but this is how you can get the selected item.

Dictionary<string, string> startupinfoDict = new Dictionary<string, string>();
    private void readfiles()
    {
        string startfolder = Environment.GetFolderPath(Environment.SpecialFolder.Startup);

        var files = Directory.GetFiles(startfolder).Where(name => !name.EndsWith(".ini"));

        foreach (string file in files)
        {
            startupinfo.Items.Add(Path.GetFileNameWithoutExtension(file));
            startupinfoDict.Add(Path.GetFileNameWithoutExtension(file), file);
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (listBox1.SelectedItem != null)
        {
            string s = listBox1.SelectedItem.ToString();

            if (startupinfoDict.ContainsKey(s))
            {
                Process.Start(startupinfoDict[s]);
            }
        }
    }
Rikki B
  • 636
  • 8
  • 23
  • Alright, I tried to open steam that is shown in my startup folder. However when it reads from listbox it only reads the name of the file IN my application dir. isn't supposed to read all information from where the item came from the beginning? – Sneakybastardd Apr 10 '13 at 23:44
  • Yes but my application is going to be used on other computers which means that many others maybe don't have steam installed on their computer :/ – Sneakybastardd Apr 11 '13 at 00:12
  • You did it! I'm not an christian but god bless you! Now I'm going to find a way to delete the file instead... Feel free to help me with that if you want? is there anyway to fill anyone's reputation? :) – Sneakybastardd Apr 11 '13 at 00:23
  • Vote up required 15 reputations. I Promise that I vote up after I got that! – Sneakybastardd Apr 11 '13 at 00:28
  • fixed that! thanks! :) I have another question if you have time! :) http://stackoverflow.com/questions/15938951/how-to-delete-a-registry-when-selecting-on-listbox – Sneakybastardd Apr 11 '13 at 00:41
1

Get the currently selected list box item and assuming an application is coupled to the file('s extension use):

Process.Start(fileName);
Michel Keijzers
  • 15,025
  • 28
  • 93
  • 119
  • Yes I know but it doesn't find whole location. It thinks it is in my application dir. It doesnt read the information from the listbox. only the name of the item – Sneakybastardd Apr 10 '13 at 23:40
  • You mean the path is not in your list box and you want to add it? In that case ... append the path to the file name from the list box and pass that to Process.Start – Michel Keijzers Apr 10 '13 at 23:41
  • no look at code that I updated with. And this pic how the listbox looks like: http://i45.tinypic.com/wt7ln8.png The button is temporary! – Sneakybastardd Apr 10 '13 at 23:49
0

You can get the selected item by using the SelectedValue property of the listbox.

You can open a file by using Process.Start.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
0

You would initiate a new process declaring the path to the file to execute / open in your button click handler:

Process.Start(@" <path to file> ");

If the selected value in the list box is the file path then:

Process.Start(<name of list box>.SelectedValue);

EDIT:

Change your foreach loop as follows:

foreach (string file in files)
{
    startupinfo.Items.Add(System.IO.Path.GetFullPath(file) + @"\" + System.IO.Path.GetFileName(file));
}
FastGeek
  • 411
  • 2
  • 7
  • That worked kinda good however I only wan't the listbox to only show name of the file at the same time, Does that work? Thank you for helping me! – Sneakybastardd Apr 11 '13 at 00:09