我如何搜索文件夹中的文件及其文件夹然后在Listview C#中打开它们 [英] How Can I Search Files In A Folder And It's Folder And Then Open Them In Listview C#
问题描述
Hey Guys,
private void button2_Click( object sender,EventArgs e)
{
尝试
{
// 检查目录是否存在
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 在文件中)
{
if (f == item.Text)
{
ext = f;
}
}
Process.Start( explorer.exe,Path.GetDirectoryName(ext));
}
}
else
MessageBox.Show( KLASÖRBULUNAMADI);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, HATA);
}
}
喜欢我的代码,我正在尝试搜索C:\ data文件夹中的文件及其子文件夹。然后我想在listview1窗口中列出它们并在我点击它们时打开这些文件夹。可能吗?对于此代码,我的listview上的文件路径列表会自动打开它们的目录。所有我需要的是当我双击它们时打开他们的目录。
没有错误消息。它正在工作,但当我点击按钮,路径上列表视图会自动打开。我想通过点击listview上的路径打开这些文件夹
文件正在自动打开因为你正在执行:Process.Start( explorer.exe,Path.GetDirectoryName(ext) );
取出那行代码,并为ListView SelectedIndexChanged事件写一个处理程序:在那个EventHandler做正确的事情打开文件基于哪个ListViewItem(s)是/点击。
为了消除与每个ListViewItem的Value对应的文件路径数据的复杂查找,我会使用一个Dictionary ...就像这样: pre lang =c#> 私人字典< listviewitem,> dctLVItemToString = new 字典< listviewitem,> ();
// 模拟:ListView和词典的示例初始化
for ( int i = 0 ; i < 10 ; i ++)
{
string iStr = i.ToString();
ListViewItem lvItem = new ListViewItem(iStr);
listView1.Items.Add(lvItem);
// 此处模拟的文件路径
dctLVItemToString.Add( lvItem, file + iStr);
}
// 在ListView SelectedIndexChanged EventHandler中使用Dictionary的示例:
private void listView1_SelectedIndexChanged( object sender,EventArgs e)
{
ListView.SelectedListViewItemCollection selectedItems = listView1.SelectedItems;
if (selectedItems.Count!= 0 )
{
string filePath = dctLVItemToString [selectedItems [ 0 ]];
// 仅用于演示目的
MessageBox.Show(文件路径);
}
}想法是简化和加速查询。
Hey Guys,
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.
"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: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: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.
这篇关于我如何搜索文件夹中的文件及其文件夹然后在Listview C#中打开它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!