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; using System.Xml; namespace FileTree { public partial class TreeForm : Form { MySorter sorter = new MySorter (); Bitmap picture = null; public TreeForm () { InitializeComponent (); listView.ListViewItemSorter = sorter; runButton_Click(null, null); /* string [] pole = { "a", "bb", "CCC" }; dataBox.ColumnCount = pole.Length; dataBox.Rows.Add(pole); dataBox.Rows.Add(pole); */ } private void listView_ColumnClick(object sender, ColumnClickEventArgs e) { if (sorter.column_index == e.Column) { sorter.inv = ! sorter.inv; } else { sorter.column_index = e.Column; sorter.inv = false; } listView.Sort (); } private void runButton_Click(object sender, EventArgs e) { DriveInfo [] drives = DriveInfo.GetDrives (); foreach ( DriveInfo d in drives ) { MyTreeNode n = new MyTreeNode (); n.Text = d.Name; n.Path = d.Name; try { // n.ToolTipText = d.VolumeLabel; } catch ( Exception ex ) { n.ToolTipText = ex.Message; } treeView.Nodes.Add (n); // addSubdirs (n, 2); } } private void addSubdirs (MyTreeNode top, int level) { if (top.ready) { if (level > 1) foreach (MyTreeNode n in top.Nodes) addSubdirs (n, level-1); } else { top.ready = true; try { DirectoryInfo top_dir = new DirectoryInfo (top.Path); DirectoryInfo [] subdirs = top_dir.GetDirectories (); foreach (DirectoryInfo d in subdirs) { MyTreeNode n = new MyTreeNode (); n.Text = d.Name; n.ToolTipText = d.FullName; n.Path = d.FullName; top.Nodes.Add (n); showStatus (n.Path); if ( level > 1 ) addSubdirs (n, level-1); } } catch (Exception ex) { // top.ToolTipText = ex.Message; showStatus (ex.Message); } } } private void showFiles (MyTreeNode top) { listView.Items.Clear (); try { DirectoryInfo top_dir = new DirectoryInfo (top.Path); FileInfo [] files = top_dir.GetFiles (); foreach (FileInfo f in files) { MyListNode n = new MyListNode (); n.FilePath = f.FullName; n.FileName = f.Name; n.FileSize = f.Length; n.FileTime = f.LastWriteTime; n.Text = n.FileName; n.ToolTipText = n.FilePath; n.SubItems.Add("" + n.FileSize); n.SubItems.Add(n.FileTime.ToString ("dd-MM-yyyy hh:mm:ss")); listView.Items.Add (n); } } catch ( Exception ex ) { // top.ToolTipText = ex.Message; showStatus (ex.Message); } } private void showFile (MyListNode n) { if (n != null) { string ext = Path.GetExtension (n.FileName); ext = ext.ToLower(); switch (ext) { case ".txt": case ".bat": case ".pas": case ".c": case ".cpp": richTextBox.LoadFile(n.FilePath, RichTextBoxStreamType.PlainText); tabControl.SelectedTab = textTab; break; case ".rtf": richTextBox.LoadFile(n.FilePath); tabControl.SelectedTab = textTab; break; case ".bmp": case ".jpg": case ".jpeg": picture = new Bitmap(n.FilePath); pictureBox_SizeChanged(null, null); tabControl.SelectedTab = pictureTab; break; case ".htm": case ".html": htmlBox.Url = new Uri ("file://" + n.FilePath); tabControl.SelectedTab = htmlTab; break; case ".csv": readDataFile(n.FilePath); tabControl.SelectedTab = dataTab; break; case ".xml": readXmlFile(n.FilePath); tabControl.SelectedTab = xmlTab; break; } } } private void readXmlFile (string filePath) { TreeNode current = null; // using System.Xml; XmlTextReader f = new XmlTextReader(filePath); while (f.Read()) { if (f.NodeType == XmlNodeType.Element) { TreeNode n = new TreeNode(); n.Text = f.Name; if (current == null) xmlBox.Nodes.Add (n); else current.Nodes.Add (n); current = n; if (f.HasAttributes) { f.MoveToFirstAttribute(); for (int i = 0; i < f.AttributeCount; i++) { TreeNode a = new TreeNode(); a.Text = "Attribute: " + f.Name + "=" + f.Value; current.Nodes.Add(a); f.MoveToNextAttribute(); } f.MoveToElement(); } if (f.IsEmptyElement) current = current.Parent; } else if (f.NodeType == XmlNodeType.EndElement) { current = current.Parent; } else if (f.NodeType == XmlNodeType.Text) { TreeNode n = new TreeNode(); n.Text = f.Value; current.Nodes.Add(n); } } } private void readDataFile (string filePath) { dataBox.Rows.Clear(); dataBox.ColumnCount = 0; StreamReader r = new StreamReader(filePath); string s = r.ReadLine (); while (s != null) { string[] pole = s.Split(','); int k = pole.Length; if (dataBox.ColumnCount < k) dataBox.ColumnCount = k; dataBox.Rows.Add(pole); s = r.ReadLine(); } /* StringBuilder txt = new StringBuilder (""); List list = new List (); int n = r.Read (); while (n >= 0) { char c = (char) n; if (c >= ' ' && c != ',' && c != ';' ) txt.Append (c); else if (c == ',' || c == ';') { list.Add (txt.ToString ()); txt = new StringBuilder (""); } else if (c == '\n') { int k = list.Count; if (dataBox.ColumnCount < k) dataBox.ColumnCount = k; dataBox.Rows.Add(list.ToArray ()); txt = new StringBuilder (""); list.Clear (); } n = r.Read(); } */ } private void showStatus(string s) { statusField.Text = s; } private void treeView_BeforeSelect (object sender, TreeViewCancelEventArgs e) { MyTreeNode n = e.Node as MyTreeNode; addSubdirs (n, 2); showStatus ("Before select " + n.Path); } private void treeView_BeforeExpand (object sender, TreeViewCancelEventArgs e) { MyTreeNode n = e.Node as MyTreeNode; addSubdirs (n, 2); showStatus ("Before expand " + n.Path); } private void treeView_AfterSelect (object sender, TreeViewEventArgs e) { MyTreeNode n = e.Node as MyTreeNode; showFiles (n); showStatus ("Selected " + n.Path); } private void listView_DoubleClick(object sender, EventArgs e) { MyListNode n = null; if (listView.SelectedItems.Count != 0) n = listView.SelectedItems [0] as MyListNode; showFile (n); } private void pictureBox_SizeChanged(object sender, EventArgs e) { if (picture != null) { int w = pictureBox.Width; int h = pictureBox.Height; int w0 = picture.Width; int h0 = picture.Height; int h1 = (int) ( (float) w * h0 / w0 ); if (h > h1) h = h1; else w = (int)((float)h * w0 / h0); pictureBox.Image = new Bitmap(picture, w, h); } } } }