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
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
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);
}
}
}
}