Click here to Skip to main content
15,888,019 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey Guys,

C#
private void button2_Click(object sender, EventArgs e)
 {
     try
     {
         // Check if directory is present or not
         if (Directory.Exists(@"C:\data"))
         {
             string names = textBox1.Text + "(" + textBox2.Text + ")" + ".jpg";

             string[] files = Directory.GetFiles(@"c:\data", names, SearchOption.AllDirectories);


             foreach (string file in files)
             {
                 string filename = Path.GetFileName(file);
                 string ext = Path.GetFileNameWithoutExtension(filename);


                 ListViewItem item = new ListViewItem(file);

                 listView1.Items.Add(item);



                 foreach (ListViewItem i in listView1.Items)
                 {
                     if (i.Selected)
                     {
                         item = i;
                     }
                 }

                     foreach (string f in files)
                     {
                         if (f == item.Text)
                         {
                             ext = f;
                         }


                     }
              Process.Start("explorer.exe", Path.GetDirectoryName(ext));





             }


         }





         else
             MessageBox.Show("KLASÖR BULUNAMADI");
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, "HATA");
     }


 }




like on my code, i'm trying to searching files on C:\data folder and it's subfolders. Then i want to list them on my listview1 window and open these folders when i click on them. Is it possible? For this code, filepaths list on my listview and open their directories automatically. All i need is open their directories when i double click on them.
Posted
Updated 19-Dec-14 2:02am
v4
Comments
Sid_Joshi 19-Dec-14 7:14am    
It's Possible
Tomas Takac 19-Dec-14 7:56am    
So tell us, where exactly are you stuck?
Member 10587827 19-Dec-14 9:17am    
i updated my question :) For this updated code, filepaths list on my listview and open their directories automatically. All i need is open their directories when i double click on them.
Afzaal Ahmad Zeeshan 19-Dec-14 8:44am    
You should reply to their comment in order to notify them.
Member 10587827 19-Dec-14 9:17am    
thanx

1 solution

"there is no error messages. it is working but when i click on button, paths on the listview are opening automatically. I want to open these folders by clicking on their path on listview"

The files are opening "autotmatically" because you are executing:
C#
Process.Start("explorer.exe", Path.GetDirectoryName(ext));
Take that line of code out, and write a handler for the ListView SelectedIndexChanged Event: in that EventHandler "do the right thing" to open the file(s) based on which ListViewItem(s) was/were clicked on.

To eliminate complex look-up of the filepath data that correspond to each ListViewItem's Value, I would use a Dictionary ... like this:
C#
private Dictionary<listviewitem,> dctLVItemToString = new Dictionary<listviewitem,>(); 

// simulation: sample initialization of a ListView and the Dictionary
for (int i = 0; i < 10; i++)
{
    string iStr = i.ToString();

    ListViewItem lvItem = new ListViewItem(iStr);

    listView1.Items.Add(lvItem);

    // file path simulated here
    dctLVItemToString.Add(lvItem, "file " + iStr);
}

// sample use of the Dictionary in the ListView SelectedIndexChanged EventHandler:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
    ListView.SelectedListViewItemCollection selectedItems = listView1.SelectedItems;

    if (selectedItems.Count != 0)
    {
        string filePath = dctLVItemToString[selectedItems[0]];

        // for demo purposes only
        MessageBox.Show(filePath);
    }
}
The idea is to simplify, and speed-up, look-up.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900