Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I want to make a small program in C # that allows to load a list with the path of all folders and subfolders of any unit, eg "C: \", to an XML or any other file type. The goal is that after this, the file will serve to another program that I have to do a search and open particular folder.

It is possible to do this?

Thanks,
Posted
Comments
CHill60 6-May-15 6:09am    
Yes.
Florian Braun 6-May-15 6:40am    
+5
OriginalGriff 6-May-15 6:40am    
As Chill60 says: yes, it's possible.

Now, what have you tried?
Where are you stuck?
What help do you need?

1 solution

Have a look here: How to: Enumerate Directories and Files[^]

Now, use XDocument class[^] to write data to xml file.
C#
string dirPath = @"E:\DOWNLOAD\";
var dirs = Directory.EnumerateDirectories(dirPath)
           .Where(d=>d.Contains("codeproject"));

var files = dirs.SelectMany(d=>Directory.EnumerateFiles(d, "*.pdf", SearchOption.AllDirectories));

XDocument xdoc = new XDocument();
XElement xRoot  = new XElement("Files");
foreach (var f in files)
{
    xRoot.Add(new XElement("File", f));
}
xdoc.Add(xRoot);
xdoc.Save("FullFileNameAndPath.xml");


Note: it's just simple sample, for large amount of data it might take a long time.
 
Share this answer
 
Comments
Member 11401516 6-May-15 17:39pm    
Thank you,

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 ???

Here's the code I have:
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;
namespace myapfind
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void txt_procura_TextChanged(object sender, EventArgs e)
{

}

private void btn_procura_Click(object sender, EventArgs e)
{
string toFind = txt_procura.Text;
var root = new DirectoryInfo(@"C:\");

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


}
}

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