Jump to content
alien

[C#] Tree view file explorer(anul 2 facultate)

Recommended Posts

Posted

Simple demo C# program made in college to demonstrate tree view file explorer.

Poate va ajuta pe astia care sunteti anul 2 pe la info ;)

hg clone https://bitbucket.org/rokill3r/filetreeview


using System;
using System.Collections;
using System.Windows.Forms;
using System.Drawing;
using System.IO;

namespace cd_tree
{
public class FileTree : TreeView
{


public FileTree()
{

GenerateImage();
Fill();
}
const int HD = 0;
const int FOLDER = 1;
const int GLMODEL = 2;
const int OTHERS = 3;
protected void GenerateImage()
{
ImageList list = new ImageList();
list.Images.Add(new Icon("ico/hd.ico"));
list.Images.Add(new Icon("ico/folder.ico"));
list.Images.Add(new Icon("ico/glmodel.ico"));
list.Images.Add(new Icon("ico/otherfiles.ico"));

ImageList = list;
}
public void Fill()
{
BeginUpdate();
string[] drives = Directory.GetLogicalDrives();
for (int i = 0; i < drives.Length; i++)
{
DirTreeNode dn = new DirTreeNode(drives[i]);
dn.ImageIndex = HD;

Nodes.Add(dn);
}
EndUpdate();

// modify the tree JIT
BeforeExpand += new TreeViewCancelEventHandler(prepare);
AfterCollapse += new TreeViewEventHandler(clear);
AfterCheck+=new TreeViewEventHandler(prepare_check);
}
public void Open(string path)
{
TreeNodeCollection nodes = Nodes;
DirTreeNode subnode = null;
int i, n;

path = path.ToLower();
nodes = Nodes;
while (nodes != null)
{
n = nodes.Count;
for (i = 0; i < n; i++)
{
subnode = (DirTreeNode)nodes[i];
if (path == subnode.Path)
{
subnode.Expand();
return;
}
if (path.StartsWith(subnode.Path))
{
subnode.Expand();
break;
}
}
if (i == n)
return;
nodes = subnode.Nodes;
}
}
void prepare(object sender, TreeViewCancelEventArgs e)
{
BeginUpdate();
DirTreeNode tn = (DirTreeNode)e.Node;
try
{

tn.populate();
EndUpdate();
}
catch (Exception ex)
{
EndUpdate();
MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace,
"Problems");
}
}
void prepare_check(object sender, TreeViewEventArgs e)
{
BeginUpdate();
DirTreeNode tn = (DirTreeNode)e.Node;
try
{
tn.Expand();
tn.populate();
EndUpdate();
}
catch (Exception ex)
{
EndUpdate();
MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace,
"Problems");
}
}
void clear(object sender, TreeViewEventArgs e)
{
BeginUpdate();
DirTreeNode tn = (DirTreeNode)e.Node;
tn.setLeaf(true);
EndUpdate();
}

public class DirTreeNode : TreeNode
{
string path;
int type;
public virtual string Path { get { return path; } }

public DirTreeNode(string s)
: base(s)
{
path = s.ToLower();
setLeaf(true);
type = HD;
ImageIndex = type;
SelectedImageIndex = type;
}
DirTreeNode(string s, int aType)
: base(new FileInfo(s).Name)
{
path = s.ToLower();
type = aType;
ImageIndex = type;
SelectedImageIndex = type;
if (type == FOLDER || type == GLMODEL)
setLeaf(true);
}

public bool IsSpecialFile(string s)
{
if (!s.EndsWith(".dll"))
return false;
ImageIndex = GLMODEL;
return true;
}

internal void populate()
{
if (type != FOLDER && type != HD)
return;
ArrayList folder = new ArrayList();
ArrayList special = new ArrayList();
ArrayList other = new ArrayList();

string[] files = Directory.GetFileSystemEntries(Path);
for (int i = 0; i < files.Length; i++)
{
FileAttributes fa = File.GetAttributes(files[i]);
if ((fa & FileAttributes.Directory) != 0)
folder.Add(new DirTreeNode(files[i], FOLDER));
else
{
if (IsSpecialFile(files[i]))
special.Add(new DirTreeNode(files[i], GLMODEL));
else
other.Add(new DirTreeNode(files[i], OTHERS));
}
}
bool ischecked = false;
if (this.Checked == true)
ischecked = true;
// add the node
Nodes.Clear();
foreach (DirTreeNode dtn in folder)
{
if (ischecked)
dtn.Checked = true;
Nodes.Add(dtn);
}
foreach (DirTreeNode dtn in special)
{
if (ischecked)
dtn.Checked = true;
Nodes.Add(dtn);
}
foreach (DirTreeNode dtn in other)
{
if (ischecked)
dtn.Checked = true;
Nodes.Add(dtn);
}

ImageIndex = type;
}

bool isLeaf = true;
internal void setLeaf(bool
{
ImageIndex = type;
Nodes.Clear();
isLeaf = b;
if (IsExpanded)
return;
if (!isLeaf)
return;
Nodes.Add(new TreeNode());
}
}
}




static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);


Application.Run(new Form1());
}
}
}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...