-4

this is my first post here on the site :)

So basically I need a gui application that can create and save XML file containing complete hierarchy of files and folders for a specified folder.

1.Each folder should be qualified with: Folder name, Folder size (bytes) and Number of files.

2.Each file should be qualified with: File name, File size (bytes), File creation, File last access time, File last modified time.

After the XML file is created the application needs to display the entire folder hierarchy tree (by using the TreeView class).

Can anyone provide help and answer? Thanks!

Snailtamer
  • 21
  • 9
  • 1
    I'd suggest you look over the ['How to Ask a Question' FAQ](https://stackoverflow.com/help/how-to-ask). StackOverflow isn't a code writing service, you have to show what attempt you've made so far. Ask specific coding problems, not broad requirements. – sab669 May 23 '17 at 11:49
  • Thanks! :) I appreciate your comment – Snailtamer May 23 '17 at 11:54

2 Answers2

1

Your question is - can u do my application for me - but anyway.

I will give you some hints to get started with your Project.

First of all - Check out MVVM here. This will help you - to handle WPF.

1. Pick the starting folder

Then you will need a FolderPicker to start your Search

  public static string PickFolder()
    {
        var dialog = new System.Windows.Forms.FolderBrowserDialog();
        System.Windows.Forms.DialogResult result = dialog.ShowDialog();

        string folder = string.Empty;
        switch (result)
        {
            case System.Windows.Forms.DialogResult.OK: return dialog.SelectedPath;
            case System.Windows.Forms.DialogResult.Cancel: return string.Empty;
            default: return string.Empty;
        }
    }

U will need the System.Windows.Forms Assembly for this. (Project -> Add reference -> Assembly)

2. folders and files

Then you want to itterate through all folders.

Check out System.IO.Directory here

3. file information

Check out System.IO.File here - this will give you some file data and to get the file size check this out

Peter
  • 1,655
  • 22
  • 44
  • Thank you very much for your help! I need to learn how to do this kind of stuff on my own. Doing an application for me would just be too easy and dumb :) – Snailtamer May 23 '17 at 12:55
1

Try following code. Fully tested. Start with small Directory. Very large folders may take time. I updated code to speed up loading the treeview.

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;
using System.Xml.Linq;

namespace WindowsFormsApplication29
{
    public partial class Form1 : Form
    {
        XDocument doc = null;
        public Form1()
        {
            InitializeComponent();

            folderBrowserDialog1.SelectedPath = @"c:\temp";

        }

        private void buttonBrowseForFolder_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            textBoxFolderName.Text = folderBrowserDialog1.SelectedPath;
        }

        private void buttonCreateXml_Click(object sender, EventArgs e)
        {
            if(Directory.Exists(textBoxFolderName.Text))
            {
                string header = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><Directory></Directory> ";
                doc = XDocument.Parse(header);
                XElement root = doc.Root;

                CreateXmlRecursive(textBoxFolderName.Text, root);
            }
        }
        private float CreateXmlRecursive(string folder, XElement folderElement)
        {
            folderElement.SetValue(folder);

            DirectoryInfo dInfo = new DirectoryInfo(folder);

            int numberOfFiles = 0;
            float size = 0.0f;

            foreach(FileInfo fInfo in dInfo.GetFiles())
            {
                try
                {
                    float fSize = fInfo.Length;
                    size += fSize;
                    folderElement.Add(new XElement("File", new object[] {
                        new XAttribute("size",fSize),
                        new XAttribute("creationDate", fInfo.CreationTime.ToShortDateString()),
                        new XAttribute("lastAccessDate", fInfo.LastAccessTime.ToShortDateString()),
                        new XAttribute("lastModifiedDate", fInfo.LastWriteTime.ToShortDateString()),
                        fInfo.Name
                    }));
                    numberOfFiles += 1;
                }
                catch(Exception e)
                {
                    Console.WriteLine("Error : CAnnot Access File '{0}'", fInfo.Name);
                }
            }
            foreach(string subFolder in Directory.GetDirectories(folder))
            {
                XElement childDirectory = new XElement("Directory");
                folderElement.Add(childDirectory);
                float dSize =  CreateXmlRecursive(subFolder, childDirectory);
                size += dSize;
            }
            folderElement.Add(new XAttribute[] {
                new XAttribute("size", size),
                new XAttribute("numberOfFiles", numberOfFiles)
            });

            return size;
        }

        private void buttonCreateTree_Click(object sender, EventArgs e)
        {
            if (doc != null)
            {
                TreeNode rootNode = new TreeNode(doc.Root.FirstNode.ToString());
                AddNode(doc.Root, rootNode);
                treeView1.Nodes.Add(rootNode);
                treeView1.ExpandAll();
            }

        }
        private void AddNode(XElement xElement, TreeNode inTreeNode)
        {

            // An element.  Display element name + attribute names & values.
            foreach (var att in xElement.Attributes())
            {
                inTreeNode.Text = inTreeNode.Text + " " + att.Name.LocalName + ": " + att.Value;
            }
            // Add children
            foreach (XElement childElement in xElement.Elements())
            {
                TreeNode tNode = inTreeNode.Nodes[inTreeNode.Nodes.Add(new TreeNode(childElement.Value))];
                AddNode(childElement, tNode);
            }
        }

    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • Thank you for your reply! Code is pretty clear and nicely written tho I'm having problems with "folderBrowserDialog1". what you named as "folderBrowserDialog1"? sorry if this question is a bit stupid but I am a beginner, hope you will have understanding :)) Maybe because I am doing it as WPF and not as Windows Form... – Snailtamer May 23 '17 at 14:25
  • It is an object in the toolbox on the form under Dialogs. Add it like any button or textbox. Make sure you take latest code. I moved a few lines to make loading treeview faster. – jdweng May 23 '17 at 14:36
  • In WPF use object FolderBrowserDialog – jdweng May 23 '17 at 14:44
  • well, for now, I successfully can open my directory and choose a folder. I have a problem with treeView1.Nodes.Add(rootNode); and with treeView1.ExpandAll(); after I type treeView1 I cant put .Nodes() or .ExpandAll(). Why do you think that is? And the second question is related to string header = " "; I dont understand the string. – Snailtamer May 23 '17 at 15:43
  • You have to create the xml file first. Make sure you add the libraries System.Xml and System.Xml.Linq. I'm adding the xml identification line and root element "Directory" to xml. The use Root to move to the element Directory. – jdweng May 23 '17 at 16:32
  • Thanks a lot! you are really helpful. I am also interested if I could put that information about the size and everything in textboxes? And, every time I click on CreateTree it creates another set of nodes below the existing one. How to clear the existing information and every time I click CreateTree it creates a new one? – Snailtamer May 24 '17 at 10:19
  • treeView1.Nodes.Clear(); Where do you want the textboxes? – jdweng May 24 '17 at 10:23
  • Read codeproject article (with complte source code): https://www.codeproject.com/Articles/14741/Advanced-TreeView-for-NET – jdweng May 24 '17 at 10:43