添加'打开文件'所选文件的按钮

时间:2017-01-24 17:25:09

标签: c# file explorer openfiledialog filechooser

到目前为止,我已经创建了一个文件资源管理器 - 但是我想添加一个按钮,允许在单击时打开所选文件。我听说过OpenFileDialog,但它似乎只显示了我不想要的目录。有任何想法吗?这是我使用C#获得的代码。

file explorer showing specific files

 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);
    }

1 个答案:

答案 0 :(得分:1)

首先,在创建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");
                }
            }
        }
    }
}