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
{
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
{
var document = XDocument.Load(documentFilePath);
var toReturn = new List
if (document.Root != null)
{
toReturn.AddRange(document.Root.Elements("Folder").Select(folder => new DirectoryInfo(folder.Value)));
}
return toReturn;
}
public static IEnumerable
{
if (root == null) throw new ArgumentNullException("root");
if (!root.Exists) return Enumerable.Empty
return SafeEnumerateDirectoriesCore(root);
}
private static IEnumerable
{
var searchPaths = new Stack
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,
Replies
Know the answer? Post it — somebody with the same question will find it here.