0

Can anyone help me with the easiest, or most efficient way to browse through files and folders?

Background: I'm trying to create a media player application, that browses through folders and when i click on a file, it must do whatever it has to with it. So i got the file browsing part down, kind of...

This is what I have, it works, but when I open a file(.txt), close it, click on "back", and enter the same directory, then i have to click on the txt file twice just to open it again...

Also please criticise this code...im here to learn.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace MediaCenter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            CheckLogicalDrives();
        }
        private void CheckLogicalDrives()
        {
            FileBrowserPanel.Controls.Clear();
            List<DriveInfo> LogicalDrives = new List<DriveInfo>();
            foreach (DriveInfo Drives in DriveInfo.GetDrives())
            {
                LogicalDrives.Add(Drives);
            }
            for (int i = 0; i < LogicalDrives.Count; i++)
            {
                LinkLabel LabelName = new LinkLabel();
                LabelName.Name = "label " + i.ToString();
                LabelName.Text = LogicalDrives[i].Name;
                LabelName.Click += delegate(object sender, EventArgs e)
                    { CheckFilesnFolders(sender, e, LabelName.Text); };
                    FileBrowserPanel.Controls.Add(LabelName);
            }
        }
        private void CheckFilesnFolders(object sender, EventArgs e, string Folder)
        {
            FileBrowserPanel.Controls.Clear();
            List<string> FoldersInCurrentDirectory = new List<string>();
            // Gets folders in the current directory
            if (Path.GetExtension(Folder) == ".txt")
            {
                Folder = Path.GetDirectoryName(Folder);
            }
            foreach (string Folders in Directory.GetDirectories(Folder))
            {
                FoldersInCurrentDirectory.Add(Folders);
            }
            foreach (string Files in Directory.GetFiles(Folder))
            {
                FoldersInCurrentDirectory.Add(Files);
            }
            BackDirectory(Folder);
            for (int i = 0; i < FoldersInCurrentDirectory.Count; i++)
            {
                string Link = FoldersInCurrentDirectory[i].ToString();
                LinkLabel LabelName1 = new LinkLabel();
                LabelName1.Name = "label " + i.ToString();
                LabelName1.Text = FoldersInCurrentDirectory[i].Remove(0, Folder.Length).Replace("\\", "");
                LabelName1.Click += delegate
                {
                    if (Path.GetExtension(Link) == ".txt") { System.Diagnostics.Process.Start(Link); }
                    else
                        CheckLogicalDrives(sender, e, Link);
                };
                FileBrowserPanel.Controls.Add(LabelName1);
            }
        }
        private void CheckLogicalDrives(object sender, EventArgs e, string Folder)
        {
            FileBrowserPanel.Controls.Clear();
            List<string> FoldersInCurrentDirectory = new List<string>();
            foreach (string Folders in Directory.GetDirectories(Folder))
            {
                FoldersInCurrentDirectory.Add(Folders);
            }
            foreach (string Files in Directory.GetFiles(Folder))
            {
                FoldersInCurrentDirectory.Add(Files);
            }
            BackDirectory(Folder);
            for (int i = 0; i < FoldersInCurrentDirectory.Count; i++)
            {
                LinkLabel LabelName = new LinkLabel();
                LabelName.Name = "label " + i.ToString();
                string Link = FoldersInCurrentDirectory[i].ToString();
                LabelName.Text = FoldersInCurrentDirectory[i].Remove(0, Folder.Length + 1).Replace("\\", "");
                LabelName.Click += delegate
                { CheckFilesnFolders(sender, e, Link); };
                System.IO.DirectoryInfo directoryInfo =
                System.IO.Directory.GetParent(LabelName.Text);
                FileBrowserPanel.Controls.Add(LabelName);
            }
        }
        private void BackDirectory(string Folder)
        {
            LinkLabel LabelName = new LinkLabel();
            LabelName.Name = "Back";
            LabelName.Text = "Back";
            System.IO.DirectoryInfo directoryInfo = System.IO.Directory.GetParent(Folder);
            if (Folder.Length > 3)
            {
                LabelName.Click += delegate(object sender, EventArgs e)
                { CheckFilesnFolders(sender, e, directoryInfo.ToString()); };
                FileBrowserPanel.Controls.Add(LabelName);
            }
            else
            {
                LabelName.Click += delegate(object sender, EventArgs e)
                { FileBrowserPanel.Controls.Clear(); FileBrowserPanel.Controls.Add(LabelName); CheckLogicalDrives(); };
                FileBrowserPanel.Controls.Add(LabelName);
            }
        }
    }
}
2
Contributors
1
Reply
26
Views
4 Years
Discussion Span
Last Post by tinstaafl
0

You might want to look at this article about creating a Windows Explorer type interface. The examples hould be in C#. It uses a Split Container with a TreeView on one side and a ListView on the other.

Edited by tinstaafl

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.