Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I decided to do another program to test the creation of A XML file with all folder strocture of a directory. Is working well, the file is created think correctly. Here's the code:

C#
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 generate_XML_open_folder
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button_generate_XML_Click(object sender, EventArgs e)
        {
            string rootPath = combobox_rootpath.Text;
            var dir = new DirectoryInfo(rootPath);

            var doc = new XDocument(GetDirectoryXml(dir));

            doc.Save("test.xml");
        }

        private void button_open_folder_Click(object sender, EventArgs e)
        {

        }

        private void textbox_folder_to_find_TextChanged(object sender, EventArgs e)
        {

        }

        private void combobox_rootpath_SelectedIndexChanged(object sender, EventArgs e)
        {
            combobox_rootpath.Items.Clear();
            foreach (string s in Directory.GetLogicalDrives())
            {

                combobox_rootpath.Items.Add(s);


            }
        }
        public static XElement GetDirectoryXml(DirectoryInfo dir)
        {
            var info = new XElement("dir",
                           new XAttribute("name", dir.Name));

            //foreach (var file in dir.GetFiles())
            // info.Add(new XElement("file",
            //  new XAttribute("name", file.Name)));

            foreach (var subDir in dir.GetDirectories())
                info.Add(GetDirectoryXml(subDir));

            return info;
        }

    }
}


Here is an example with the content of the XML file:

XML
<?xml version="1.0" encoding="utf-8"?>
<dir name="h:\">
  <dir name="System Volume Information" />
  <dir name="xp1000" />
  <dir name="Procura_Desenhos">
    <dir name="Procura_Desenhos">
      <dir name="Resources" />
      <dir name="Properties" />
      <dir name="obj">
        <dir name="Release" />
        <dir name="Debug">
          <dir name="TempPE" />
          <dir name="Refactor" />
        </dir>
      </dir>
      <dir name="bin">
        <dir name="Release" />
        <dir name="Debug" />
      </dir>
    </dir>
  </dir>
  <dir name="Procura_Desenhos1.1">
    <dir name="Procura_Desenhos1.1">
      <dir name="bin">
        <dir name="Debug" />
        <dir name="Release" />
      </dir>
      <dir name="obj">
        <dir name="Debug">
          <dir name="TempPE" />
        </dir>
      </dir>
      <dir name="Properties" />
      <dir name="Resources" />
    </dir>
  </dir>
  <dir name="Backup">
    <dir name="Visual 2008">
      <dir name="Projects">
        <dir name="Procura_Desenhos">
          <dir name="Procura_Desenhos">
            <dir name="Resources" />
            <dir name="Properties" />
            <dir name="obj">
              <dir name="Release" />
              <dir name="Debug">
                <dir name="TempPE" />
                <dir name="Refactor" />
              </dir>
            </dir>
            <dir name="bin">
              <dir name="Release" />
              <dir name="Debug" />
            </dir>
          </dir>
        </dir>
      </dir>
    </dir>
    <dir name="Visual 2005">
      <dir name="Projects">
        <dir name="WindowsApplication1">
          <dir name="WindowsApplication1">
            <dir name="obj">
              <dir name="Release" />
              <dir name="Debug">
                <dir name="TempPE" />
              </dir>
            </dir>
            <dir name="My Project" />
            <dir name="bin">
              <dir name="Release" />
              <dir name="Debug" />
            </dir>
          </dir>
        </dir>
        <dir name="Procura">
          <dir name="Procura">
            <dir name="Resources" />
            <dir name="obj">
              <dir name="Release" />
              <dir name="Debug">
                <dir name="TempPE" />
              </dir>
            </dir>
            <dir name="My Project" />
            <dir name="bin">
              <dir name="Release" />
              <dir name="Debug" />
            </dir>
          </dir>
        </dir>
        <dir name="find_v1.1">
          <dir name="_UpgradeReport_Files" />
          <dir name="find_v1.1">
            <dir name="Properties" />
            <dir name="obj">
              <dir name="Release" />
              <dir name="Debug">
                <dir name="TempPE" />
              </dir>
            </dir>
            <dir name="bin">
              <dir name="Release" />
              <dir name="Debug" />
            </dir>
          </dir>
          <dir name="Backup">
            <dir name="find_v1.1">
              <dir name="Properties" />
            </dir>
          </dir>
        </dir>
        <dir name="find_v1.0">
          <dir name="find_v1.0">
            <dir name="Properties" />
            <dir name="obj">
              <dir name="Release" />
              <dir name="Debug">
                <dir name="TempPE" />
              </dir>
            </dir>
            <dir name="bin">
              <dir name="Release" />
              <dir name="Debug" />
            </dir>
          </dir>
        </dir>
      </dir>
    </dir>
  </dir>
</dir>


Now what I want is to enter in textbox_folder_to_find the name of the folder to look in , the program searchs in the XML file for the path of that folder and after having the path is opened with shellExecute or explorer. Does anyone know how I can do this?
Posted

1 solution

Something like this should work:
C#
var doc = XDocument.Load("test.xml");
var folderToFind = textbox_folder_to_find.Text;

var paths = doc.Descendants("dir")
    .Where(dir => string.Equals(folderToFind, (string)dir.Attribute("name"), StringComparison.OrdinalIgnoreCase))
    .Select(dir => dir.AncestorsAndSelf().Select(el => (string)el.Attribute("name")).Reverse().Aggregate(string.Empty, Path.Combine))
;

foreach (string path in paths)
{
    Process.Start(new ProcessStartInfo
    {
        FileName = path,
        UseShellExecute = true,
        Verb = "open"
    });
}



EDIT: To solve the issues you raised in the comments, you'll need to change how you generate the XML file:
C#
public static XElement GetDirectoryXml(DirectoryInfo root)
{
    if (root == null) throw new ArgumentNullException("root");
    if (!root.Exists) throw new DirectoryNotFoundException(string.Format("The directory '{0}' does not exist.", root.FullName));

    var result = new XElement("dir", new XAttribute("name", root.FullName));
    BuildDirectoryXml(root, result);
    return result;
}

private static void BuildDirectoryXml(DirectoryInfo currentPath, XElement parentElement)
{
    try
    {
        foreach (var subDirectory in currentPath.EnumerateDirectories())
        {
            var element = new XElement("dir", new XAttribute("name", subDirectory.Name));
            BuildDirectoryXml(subDirectory, element);
            parentElement.Add(element);
        }
    }
    catch (UnauthorizedAccessException)
    {
    }
}
 
Share this answer
 
v2
Comments
Member 11401516 8-May-15 10:43am    
It work's thanks again Richard. Just two more things:
1- The first dir name in the XML file should have the should have the full path. For example if I put in combobox_rootpath \\vgst\ sales, which is the path to a server folder, the first dir name of XML file it takes only "sales" and then when i make the search it says thet the folder does not exist.

2- When i put in combobox_rootpath C:\ the program returns a access to the path 'c:\System Volume Information\' is denied.

Can you help me again please!
Richard Deeming 8-May-15 14:14pm    
A slight change to the XML generation method should solve the problem. I've updated my answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900