为所选文件添加 'Open File' 按钮
Adding an 'Open File' button for selected file
到目前为止,我已经创建了一个文件资源管理器 - 但是我想添加一个按钮,该按钮将允许在单击时打开所选文件。我听说过 OpenFileDialog 但它似乎只显示了一个我不想要的目录。有任何想法吗?这是我得到的代码,使用 C#。
namespace SynchronizeTaskPaneAndRibbon
{
public partial class FileChooser : UserControl
{
private void PopulateTreeView()
{
TreeNode rootNode;
DirectoryInfo info = new DirectoryInfo(@"../..");
if (info.Exists)
{
rootNode = new TreeNode(info.Name);
rootNode.Tag = info;
GetDirectories(info.GetDirectories(), rootNode);
treeView1.Nodes.Add(rootNode);
}
}
private void GetDirectories(DirectoryInfo[] subDirs, TreeNode nodeToAddTo)
{
TreeNode aNode;
DirectoryInfo[] subSubDirs;
foreach (DirectoryInfo subDir in subDirs)
{
aNode = new TreeNode(subDir.Name, 0, 0);
aNode.Tag = subDir;
aNode.ImageKey = "folder";
try
{
subSubDirs = subDir.GetDirectories();
}
catch (System.UnauthorizedAccessException)
{
subSubDirs = new DirectoryInfo[0];
}
if (subSubDirs.Length != 0)
{
GetDirectories(subSubDirs, aNode);
}
nodeToAddTo.Nodes.Add(aNode);
}
}
public FileChooser()
{
InitializeComponent();
PopulateTreeView();
this.treeView1.NodeMouseClick += new TreeNodeMouseClickEventHandler(this.treeView1_NodeMouseClick);
}
void treeView1_NodeMouseClick(object sender,TreeNodeMouseClickEventArgs e)
{
TreeNode newSelected = e.Node;
listView1.Items.Clear();
DirectoryInfo nodeDirInfo = (DirectoryInfo)newSelected.Tag;
ListViewItem.ListViewSubItem[] subItems;
ListViewItem item = null;
foreach (DirectoryInfo dir in nodeDirInfo.GetDirectories())
{
item = new ListViewItem(dir.Name, 0);
subItems = new ListViewItem.ListViewSubItem[]
{new ListViewItem.ListViewSubItem(item, "Directory"),
new ListViewItem.ListViewSubItem(item,
dir.LastAccessTime.ToShortDateString())};
item.SubItems.AddRange(subItems);
}
foreach (FileInfo file in nodeDirInfo.GetFiles())
{
if (file.Extension == ".xlsx" || file.Extension == ".xls" || file.Extension == ".xlsm" || file.Extension == ".csv")
{
item = new ListViewItem(file.Name, 1);
subItems = new ListViewItem.ListViewSubItem[]
{ new ListViewItem.ListViewSubItem(item, "File"),
new ListViewItem.ListViewSubItem(item,
file.LastAccessTime.ToShortDateString())};
item.SubItems.AddRange(subItems);
listView1.Items.Add(item);
}
}
listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
}
首先,在创建 ListViewItems 时,我会将文件的 完整路径 添加到 .Tag
以便您可以轻松检索它:
item.Tag = file.FullName;
然后你会在按钮点击事件中使用类似这样的东西:
private void button1_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
foreach(ListViewItem lvi in listView1.SelectedItems)
{
if (lvi.Tag != null && lvi.Tag is string)
{
string fullPathFile = lvi.Tag.ToString();
try
{
Process.Start(fullPathFile);
}
catch (Exception ex)
{
MessageBox.Show("FileName: " + fullPathFile + "\r\n\r\n" + ex.ToString(), "Error Opening File");
}
}
}
}
}
到目前为止,我已经创建了一个文件资源管理器 - 但是我想添加一个按钮,该按钮将允许在单击时打开所选文件。我听说过 OpenFileDialog 但它似乎只显示了一个我不想要的目录。有任何想法吗?这是我得到的代码,使用 C#。
namespace SynchronizeTaskPaneAndRibbon
{
public partial class FileChooser : UserControl
{
private void PopulateTreeView()
{
TreeNode rootNode;
DirectoryInfo info = new DirectoryInfo(@"../..");
if (info.Exists)
{
rootNode = new TreeNode(info.Name);
rootNode.Tag = info;
GetDirectories(info.GetDirectories(), rootNode);
treeView1.Nodes.Add(rootNode);
}
}
private void GetDirectories(DirectoryInfo[] subDirs, TreeNode nodeToAddTo)
{
TreeNode aNode;
DirectoryInfo[] subSubDirs;
foreach (DirectoryInfo subDir in subDirs)
{
aNode = new TreeNode(subDir.Name, 0, 0);
aNode.Tag = subDir;
aNode.ImageKey = "folder";
try
{
subSubDirs = subDir.GetDirectories();
}
catch (System.UnauthorizedAccessException)
{
subSubDirs = new DirectoryInfo[0];
}
if (subSubDirs.Length != 0)
{
GetDirectories(subSubDirs, aNode);
}
nodeToAddTo.Nodes.Add(aNode);
}
}
public FileChooser()
{
InitializeComponent();
PopulateTreeView();
this.treeView1.NodeMouseClick += new TreeNodeMouseClickEventHandler(this.treeView1_NodeMouseClick);
}
void treeView1_NodeMouseClick(object sender,TreeNodeMouseClickEventArgs e)
{
TreeNode newSelected = e.Node;
listView1.Items.Clear();
DirectoryInfo nodeDirInfo = (DirectoryInfo)newSelected.Tag;
ListViewItem.ListViewSubItem[] subItems;
ListViewItem item = null;
foreach (DirectoryInfo dir in nodeDirInfo.GetDirectories())
{
item = new ListViewItem(dir.Name, 0);
subItems = new ListViewItem.ListViewSubItem[]
{new ListViewItem.ListViewSubItem(item, "Directory"),
new ListViewItem.ListViewSubItem(item,
dir.LastAccessTime.ToShortDateString())};
item.SubItems.AddRange(subItems);
}
foreach (FileInfo file in nodeDirInfo.GetFiles())
{
if (file.Extension == ".xlsx" || file.Extension == ".xls" || file.Extension == ".xlsm" || file.Extension == ".csv")
{
item = new ListViewItem(file.Name, 1);
subItems = new ListViewItem.ListViewSubItem[]
{ new ListViewItem.ListViewSubItem(item, "File"),
new ListViewItem.ListViewSubItem(item,
file.LastAccessTime.ToShortDateString())};
item.SubItems.AddRange(subItems);
listView1.Items.Add(item);
}
}
listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
}
首先,在创建 ListViewItems 时,我会将文件的 完整路径 添加到 .Tag
以便您可以轻松检索它:
item.Tag = file.FullName;
然后你会在按钮点击事件中使用类似这样的东西:
private void button1_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
foreach(ListViewItem lvi in listView1.SelectedItems)
{
if (lvi.Tag != null && lvi.Tag is string)
{
string fullPathFile = lvi.Tag.ToString();
try
{
Process.Start(fullPathFile);
}
catch (Exception ex)
{
MessageBox.Show("FileName: " + fullPathFile + "\r\n\r\n" + ex.ToString(), "Error Opening File");
}
}
}
}
}