C# 如何使用c窗口窗体显示从目录listview控件中选择的文件?
下面的“我的代码”显示文件夹的列表视图,该文件夹由folderBrowser控件选择。在列表视图中显示文件夹和文件后,我想打开单击的文件。我使用LISTVIEW\u itemactivation编写了以下代码。问题是我无法打开所选文件。我做错了什么C# 如何使用c窗口窗体显示从目录listview控件中选择的文件?,c#,listview,C#,Listview,下面的“我的代码”显示文件夹的列表视图,该文件夹由folderBrowser控件选择。在列表视图中显示文件夹和文件后,我想打开单击的文件。我使用LISTVIEW\u itemactivation编写了以下代码。问题是我无法打开所选文件。我做错了什么 private void PopulateListView() { listView1.Clear(); //headers listview listView1.Columns.Add("File Name", 150);
private void PopulateListView()
{
listView1.Clear();
//headers listview
listView1.Columns.Add("File Name", 150);
listView1.Columns.Add("Last Accessed", 110);
listView1.Columns.Add("Size", 100);
//listView1.ItemActivate += new System.EventHandler(listView1_ItemActivate);
if (folderBrowser.ShowDialog() == DialogResult.OK)
{
string[] files = Directory.GetFiles(folderBrowser.SelectedPath);
string[] folders = Directory.GetDirectories(folderBrowser.SelectedPath);
foreach (string file in files)
{
long folderSize = 0;
string fileName = Path.GetFileNameWithoutExtension(file);
FileInfo finfo = new FileInfo(file);
folderSize += finfo.Length;
ListViewItem item = new ListViewItem(new[] { fileName, File.GetLastAccessTime(file).ToString(), finfo.Length.ToString() });
images();
item.ImageIndex = 1;
listView1.Items.Add(item);
}
foreach (string file in folders)
{
string fileName = Path.GetFileNameWithoutExtension(file);
ListViewItem item = new ListViewItem(new[] { fileName, File.GetLastAccessTime(file).ToString(), file.Length.ToString() });
images();
item.ImageIndex = 0;
listView1.Items.Add(item);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
PopulateListView();
textBox1.Text = folderBrowser.SelectedPath;
}
private void images(){
try
{
imageList1.Images.Add(Bitmap.FromFile("./images/file.gif"));
imageList1.Images.Add(Bitmap.FromFile("./images/Folder.gif"));
}
catch (FileNotFoundException) { }
}
private void listView1_DoubleClick(object sender, EventArgs e)
{
ListViewItem item_clicked = listView1.SelectedItems[0];
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
listView1.View = View.LargeIcon;
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
listView1.View = View.SmallIcon;
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
listView1.View = View.Details;
}
private void radioButton4_CheckedChanged(object sender, EventArgs e)
{
listView1.View = View.List;
}
private void radioButton5_CheckedChanged(object sender, EventArgs e)
{
listView1.View = View.Tile;
}
private void listView1_ItemActivate(object sender, EventArgs e)
{
try
{
string sPath = listView1.SelectedItems.ToString();
string sFileName = listView1.FocusedItem.Text;
Process.Start(sPath + "\\" + sFileName);
}
catch(Exception Exc) { MessageBox.Show(Exc.ToString()); }
}
好的,根据您填充列表的方式,修改后的代码应该可以工作
private void listView1_ItemActivate(object sender, EventArgs e)
{
try
{
string sPath = listView1.SelectedItem.SubItems[0].Text;
Process.Start(sPath);
}
catch(Exception Exc)
{
MessageBox.Show(Exc.ToString());
}
}
但是您还需要去掉这一行,因为在不知道文件扩展名的情况下,您永远无法加载该文件
string fileName = Path.GetFileNameWithoutExtension(file);
请参见,当您像这样加载ListViewItem时,它将加载该数组中的所有子项。此外,Directory.getfilefolderbrowser.SelectedPath返回的数组;返回具有完整路径的文件数组,因此,如果您选择第一个子项,它将是该文件的完整路径,并因此发出进程。在该文件路径上启动将导致shell在其默认程序中打开该文件。您是否遇到异常?无异常我猜我在选择路径和文件名时做错了什么。它给出了一个异常,系统无法找到当我像这样使用它时指定的文件this@engineer41:请查看我的编辑,我想我在获得该路径的行中有一个输入错误。@engineer41:我看到问题了。加载列表时要删除扩展名,请不要这样做。string sPath=listView1.SelectedItems.Text;此语句不接受文本defination@engineer41:请注意,行已更改为listView1。SelectedItem.SubItems[0]。文本。