我是编程和 C# 的新手,似乎对上述内容有点困惑。我想要做的是为客厅媒体电脑创建一个前端,没什么好开始的,因为我知道这对于像我这样的菜鸟来说是一项艰巨的任务。我已经四处张望,并且对启动外部 exe、存储/加载资源等非常满意。我对我 2 周冲浪的结果感到非常满意。
所以我通过启动一个模拟器来开始我的项目,我想做的是扫描一个文件夹中的 zip 文件和图像文件,如果它找到匹配的图像和 zip 文件,它会在每个列表视图中显示一个图像拉链找到。
所以我像这样填充我的列表框并让我的 2 个列表框显示我想看到的东西。
PopulateListBox(listBox1, "\\SomePath\\", "*.zip");
PopulateListBox(listBox2, "\\Images\\", "*.jpg");
private void PopulateListBox(ListBox lsb, string Folder, string FileType)
{
DirectoryInfo dinfo = new DirectoryInfo(Folder);
FileInfo[] Files = dinfo.GetFiles(FileType);
foreach (FileInfo file in Files)
{
lsb.Items.Add(file.Name);
}
}
所以我现在有我的 2 个列表框,可以看到我有 game1.zip 和 game1.jpg,现在我可以用 game1 图像填充我的列表视图并启动他说的简单的模拟器。
这就是我当前填充列表视图的方式。
PopulateListView();
private void PopulateListView()
{
if (listBox1.Items.Contains("game1.zip"))
{
if (File.Exists("\\Images\\game1.jpg"))
{
imageList1.Images.Add(Image.FromFile("\\Images\\game1.jpg"));
listView1.Items.Add("", 0);
}
}
if (listBox1.Items.Contains("game2.zip"))
{
if (File.Exists("\\Images\\game2.jpg"))
{
imageList1.Images.Add(Image.FromFile("\\Images\\game2.jpg"));
listView1.Items.Add("", 1);
}
}
}
这就是我目前正在启动的方式,并且工作正常。
// launch item
private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (listView1.Items[0].Selected == true)
{
string rom = "\\" + listBox1.Items[0].ToString();
// Launch code in here
}
if (listView1.Items[1].Selected == true)
{
string rom = "\\" + listBox1.Items[1].ToString();
// Launch code in here
}
}
那么你可能会问什么问题?如果可能的话,我不想继续为每个项目输入所有信息,我想使用某种陈述,我不知道要搜索什么没有帮助。图像名称将始终与 zip 名称匹配,只需要松开文件扩展名,以便像这样填充列表视图。
if (listbox1 item = listbox2 item)
{
add to imagelist and listview automaticly with same index
}
然后我希望能够使用这样的东西启动。
private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
string rom = "\\" + listview.selected.item.tostring;
// Launch code in here
}
希望我在我的智慧尽头时说得通。
问候
德里克