在C#Listview中显示特定文件类型的文件夹内容
我正在使用Visual C#2010 Express。我试图做的是列出特定文件类型的预定义目录中的所有文件。每个列表旁边都有一个复选框。因此,可以在所有标记的文件上单独运行相同的命令 在过去,我使用cmd.exe批处理脚本来执行类似的操作。但我不知道如何把它翻译成C。用于System.Windows.Forms.ListView。至少我认为我想使用ListView。我也不知道如何在每个文件名列表的开头添加复选框 批处理示例:在C#Listview中显示特定文件类型的文件夹内容,c#,winforms,C#,Winforms,我正在使用Visual C#2010 Express。我试图做的是列出特定文件类型的预定义目录中的所有文件。每个列表旁边都有一个复选框。因此,可以在所有标记的文件上单独运行相同的命令 在过去,我使用cmd.exe批处理脚本来执行类似的操作。但我不知道如何把它翻译成C。用于System.Windows.Forms.ListView。至少我认为我想使用ListView。我也不知道如何在每个文件名列表的开头添加复选框 批处理示例: @echo off REM Path variable set for
@echo off
REM Path variable set for sake of example.
set "path=c:\temp"
:fList REM File List Loop
SETLOCAL EnableDelayedExpansion
For /F "delims=" %%A In ( ' DIR /B /O:N /A:-D "%path%\*.ext" ' ) Do (
echo %%A
)
goto :EOF
提前感谢您提供的任何帮助或建议阅读材料。要在目录中获取文件夹,请执行以下操作:
string myDir = @"c:\";
string wildcard = "*.ext";
var files = System.IO.Directory.GetFiles(myDir,wildcard);
为了将这些文件添加到列表视图,这是一个不错的起点链接:
void CreateMyListView()
{
// Create a new ListView control.
ListView listView1 = new ListView();
listView1.Bounds = new Rectangle(new Point(10,10), new Size(300,200));
// Set the view to show details.
listView1.View = View.Details;
// Allow the user to edit item text.
listView1.LabelEdit = true;
// Allow the user to rearrange columns.
listView1.AllowColumnReorder = true;
// Display check boxes.
listView1.CheckBoxes = true;
// Select the item and subitems when selection is made.
listView1.FullRowSelect = true;
// Display grid lines.
listView1.GridLines = true;
// Sort the items in the list in ascending order.
listView1.Sorting = SortOrder.Ascending;
// Create three items and three sets of subitems for each item.
ListViewItem item1 = new ListViewItem("item1",0);
// Place a check mark next to the item.
item1.Checked = true;
item1.SubItems.Add("1");
item1.SubItems.Add("2");
item1.SubItems.Add("3");
ListViewItem item2 = new ListViewItem("item2",1);
item2.SubItems.Add("4");
item2.SubItems.Add("5");
item2.SubItems.Add("6");
ListViewItem item3 = new ListViewItem("item3",0);
// Place a check mark next to the item.
item3.Checked = true;
item3.SubItems.Add("7");
item3.SubItems.Add("8");
item3.SubItems.Add("9");
// Create columns for the items and subitems.
listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center);
//Add the items to the ListView.
listView1.Items.AddRange(new ListViewItem[]{item1,item2,item3});
// Create two ImageList objects.
ImageList imageListSmall = new ImageList();
ImageList imageListLarge = new ImageList();
// Initialize the ImageList objects with bitmaps.
imageListSmall.Images.Add(Bitmap.FromFile("C:\\MySmallImage1.bmp"));
imageListSmall.Images.Add(Bitmap.FromFile("C:\\MySmallImage2.bmp"));
imageListLarge.Images.Add(Bitmap.FromFile("C:\\MyLargeImage1.bmp"));
imageListLarge.Images.Add(Bitmap.FromFile("C:\\MyLargeImage2.bmp"));
//Assign the ImageList objects to the ListView.
listView1.LargeImageList = imageListLarge;
listView1.SmallImageList = imageListSmall;
// Add the ListView to the control collection.
this.Controls.Add(listView1);
}
要获取文件,如果只有一个扩展名,可以使用
Directory.GetFiles(path,extension)
。在我的示例中,我创建了一个处理多个扩展的方法
您也可以使用CheckedListBox代替ListView,它已经有了所需的复选框。
您可以获取文件(具有所需的扩展名)并按如下方式填写checkedListBox:
private void Form1_Load(object sender, EventArgs e)
{
const string path = @"c:/yourpath";
List<string> extensions = new List<string> { "EXE", "PNG" };
string[] files = GetFilesWithExtensions(path, extensions);
checkedListBox1.Items.AddRange(files);
}
private string[] GetFilesWithExtensions(string path, List<string> extensions)
{
string[] allFilesInFolder = Directory.GetFiles(path);
return allFilesInFolder.Where(f => extensions.Contains(f.ToUpper().Split('.').Last())).ToArray();
}
private void button1_Click(object sender, EventArgs e)
{
CheckedListBox.CheckedItemCollection selectedFiles = checkedListBox1.CheckedItems;
//Do stuff with files
}
一旦用户检查了所需的文件,例如单击按钮,您就可以获得如下所示的选定元素:
private void Form1_Load(object sender, EventArgs e)
{
const string path = @"c:/yourpath";
List<string> extensions = new List<string> { "EXE", "PNG" };
string[] files = GetFilesWithExtensions(path, extensions);
checkedListBox1.Items.AddRange(files);
}
private string[] GetFilesWithExtensions(string path, List<string> extensions)
{
string[] allFilesInFolder = Directory.GetFiles(path);
return allFilesInFolder.Where(f => extensions.Contains(f.ToUpper().Split('.').Last())).ToArray();
}
private void button1_Click(object sender, EventArgs e)
{
CheckedListBox.CheckedItemCollection selectedFiles = checkedListBox1.CheckedItems;
//Do stuff with files
}
那么你是从零开始的,对C#或Windows窗体编程一无所知?如果是这样,那么我会从谷歌开始寻找一些教程。如果没有,那么请向我们展示您目前掌握的C#代码。就控制台应用程序而言,我对C#有基本的工作知识。不过,我或多或少只是刚刚开始使用Windows窗体。只是想在我一直在做的事情上添加一个GUI。然而,我没有任何关于这个特殊困境的预先存在的代码。这是一个新的添加。您可以使用
directory.GetFiles(“%path%”,“*.ext”)
获取目录中的文件,将它们添加到CheckedListBox
,然后迭代CheckedListBox.CheckedItems
属性以执行命令。非常感谢。这很好地发挥了作用。说明和示例清晰简洁。