Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8061287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:17:19+00:00 2026-06-05T10:17:19+00:00

I need to walk a folder tree in C# and somehow record what I

  • 0

I need to walk a folder tree in C# and somehow record what I find, then pass it as a string to another device on my network, where I will display it graphically.

Walking the folder tree is simple with recursion, as is passing the string along.

However I would like the format of the string to be as portable as possible, so I thought of XML. I’m guessing I can somehow serialise the XML to a string.

I’m very new to XML, so I’m unsure how best to proceed. I’m thinking the format should end up something like this example:

<Tree>
  <Folder Name="Folder1">
    <File Name="File1" />
  </Folder>
</Tree>

Any ideas? Do I use LinqToXML to build the string out of XElement objects like an example I’ve seen, or is that not the best way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T10:17:20+00:00Added an answer on June 5, 2026 at 10:17 am

    Ok so I went ahead with XElement – turns out it’s as easy as Chuck Savage said 🙂

    private void BuildFolderTree(DirectoryInfo parentFolder, XElement parentElement)
    {
        // Find all the subfolders under this folder.
        foreach (DirectoryInfo folderInfo in parentFolder.GetDirectories())
        {
            // Add this folder to the doc.
            XElement folderElement = new XElement("Folder", new XAttribute("Name", folderInfo.Name), new XAttribute("Path", folderInfo.FullName));
                    parentElement.Add(folderElement);
    
            // Recurse into this folder.
            BuildFolderTree(folderInfo, folderElement);
        }
    
        // Process all the files in this folder
        foreach (FileInfo fileInfo in parentFolder.GetFiles("*.*"))
        {
            // Add this file minus its extension.
            parentElement.Add(new XElement(STR_File, new XAttribute("Name", fileInfo.Name), new XAttribute("Path", fileInfo.FullName)));
        }
    }
    
    // main code
    DriveInfo di = new DriveInfo("M");
    XElement usbKeyTreeElement = new XElement("USBKey");
    BuildFolderTree(di.RootDirectory, usbKeyTreeElement);
    string usbKeyString = usbKeyTreeElement.ToString();
    

    usbKeyString ends up looking something like this:

    <USBKey>
      <Folder Name="folder1" Path="M:\folder1" />
      <Folder Name="folder2" Path="M:\folder2">
        <File Name="file1" Path="M:\folder2\file1.txt" />
        <File Name="file2" Path="M:\folder2\file2.txt" />
      </Folder>
    </USBKey>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to walk through a JDOM tree and extract all data from body
Need to take a SELECT drop down list options and find if any of
need help to create regular expression matching string www.*.abc.*/somestring Here * is wild card
I need to walk over an array, and conditionally delete elements, in Perl. I
I need a simple AI script to make a zombie object 'walk' around randomly.
I need to extract the web address from this string: <p> Feb 24 -
I need to walk or interact across all nodes and child nodes with VB.NET
I need a very basic step by step walk through on calling a webservice
I need to build a little wizard that looks like this where you walk
I need to generate a random walk based on the DNA sequence of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.