Directory folders and subfolders tree to XML
I have a program that does a search and opens determined folder inserted in the textbox. Works well, however, the search is a little slow ... So I wanted to add a button to first pass the tree of folders and subfolders to an XML file and then through this file do the search and open the folder previously inserted in the textbox . I'm not sure if this method can accelerate the search ?
I made some changes and the XML is created but is only filed with the path of the folder that i have inserted in textbox... What I want is to put the path in combobox, click the button to generate XML document, the document is filled with all paths of folders and subfolders in that drive, for example every paths of directory "C: \\". Then enter in the textbox the folder to look in, click on the browse button and this folder is opened with the UseShellExecute.
Here's what I have so far:
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.Diagnostics;
using System.Xml;
using System.Xml.Linq;
namespace find_program
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void txt_procura_TextChanged(object sender, EventArgs e)
{
}
private void btn_sair_Click(object sender, EventArgs e)
{
this.Close();
}
private void btn_procura_Click(object sender, EventArgs e)
{
string toFind = txt_procura.Text;
var root = new DirectoryInfo(caminho_procura.Text);
var matchingDirectories = SafeEnumerateDirectories(root)
.Where(d => string.Equals(d.Name, toFind, StringComparison.OrdinalIgnoreCase))
;
foreach (DirectoryInfo directory in matchingDirectories)
{
Process.Start(new ProcessStartInfo
{
FileName = directory.FullName,
UseShellExecute = true,
Verb = "open"
});
}
//Saving the directory list
//Pass your matching directories here. Probably "matchingDirectories?"
var document = GetXmlDocument(matchingDirectories);
document.Save("tree.xml"); //SAVES YOUR FOLDER LIST TO AN XML FILE
var folderList = GetFolderList("tree.xml");
}
private void btn_XML_Click(object sender, EventArgs e)
{
}
public XDocument GetXmlDocument(IEnumerable<DirectoryInfo> matchingDirectories)
{
var document = new XDocument(new XElement("FolderList"));
var root = document.Root;
foreach (var directory in matchingDirectories)
{
var foundFolder = new XElement("Folder", directory.FullName);
root.Add(foundFolder);
}
return document;
}
public IEnumerable<DirectoryInfo> GetFolderList(string documentFilePath)
{
var document = XDocument.Load(documentFilePath);
var toReturn = new List<DirectoryInfo>();
if (document.Root != null)
{
toReturn.AddRange(document.Root.Elements("Folder").Select(folder => new DirectoryInfo(folder.Value)));
}
return toReturn;
}
public static IEnumerable<DirectoryInfo> SafeEnumerateDirectories(DirectoryInfo root)
{
if (root == null) throw new ArgumentNullException("root");
if (!root.Exists) return Enumerable.Empty<DirectoryInfo>();
return SafeEnumerateDirectoriesCore(root);
}
private static IEnumerable<DirectoryInfo> SafeEnumerateDirectoriesCore(DirectoryInfo root)
{
var searchPaths = new Stack<DirectoryInfo>();
searchPaths.Push(root);
while (searchPaths.Count != 0)
{
DirectoryInfo currentPath = searchPaths.Pop();
yield return currentPath;
DirectoryInfo[] subFolders = null;
try
{
foreach (var subDirectory in currentPath.EnumerateDirectories())
{
searchPaths.Push(subDirectory);
}
}
catch (UnauthorizedAccessException)
{
}
}
}
private void caminho_procura_SelectedIndexChanged(object sender, EventArgs e)
{
caminho_procura.Items.Clear();
foreach (string s in Directory.GetLogicalDrives())
{
caminho_procura.Items.Add(s);
}
}
}
}
Thanks,