0

So far I have created a File Explorer - however I want to add a button which will allow the selected file to be opened when clicked. I've heard about the OpenFileDialog but it only seems to show a directory which I don't want. Any ideas? This is the code I've got, using 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);
    }
DoN_Dan
  • 69
  • 1
  • 1
  • 8
  • Pass the full path to your file into [Process.Start()](https://msdn.microsoft.com/en-us/library/53ezey2s(v=vs.110).aspx). The application associated with that file's extension will receive that filename. – Idle_Mind Jan 24 '17 at 17:29
  • Thanks @Idle_Mind how would I go about implementing this into what I've currently got? – DoN_Dan Jan 24 '17 at 17:59

1 Answers1

1

First, when creating the ListViewItems, I'd add the full path of the file to the .Tag so you can retrieve it easily:

item.Tag = file.FullName;

Then you'd use something like this in a button click event:

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");
                }
            }
        }
    }
}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40