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 757221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:20:14+00:00 2026-05-14T15:20:14+00:00

The end goal is to have some form of a data structure that stores

  • 0

The end goal is to have some form of a data structure that stores a hierarchal structure of a directory to be stored in a txt file.

I’m using the following code and so far, and I’m struggling with combining dirs, subdirs, and files.

/// <summary>
/// code based on http://msdn.microsoft.com/en-us/library/bb513869.aspx
/// </summary>
/// <param name="strFolder"></param>
public static void TraverseTree ( string strFolder )
{
  // Data structure to hold names of subfolders to be
  // examined for files.
  Stack<string> dirs = new Stack<string>( 20 );

  if ( !System.IO.Directory.Exists( strFolder ) )
  {
    throw new ArgumentException();
  }
  dirs.Push( strFolder );

  while ( dirs.Count > 0 )
  {
    string currentDir = dirs.Pop();
    string[] subDirs;
    try
    {
      subDirs = System.IO.Directory.GetDirectories( currentDir );
    }

    catch ( UnauthorizedAccessException e )
    {
      MessageBox.Show( "Error: " + e.Message );
      continue;
    }
    catch ( System.IO.DirectoryNotFoundException e )
    {
      MessageBox.Show( "Error: " +  e.Message );
      continue;
    }

    string[] files = null;
    try
    {
      files = System.IO.Directory.GetFiles( currentDir );
    }

    catch ( UnauthorizedAccessException e )
    {
      MessageBox.Show( "Error: " +  e.Message );
      continue;
    }

    catch ( System.IO.DirectoryNotFoundException e )
    {
      MessageBox.Show( "Error: " + e.Message );
      continue;
    }
    // Perform the required action on each file here.
    // Modify this block to perform your required task.
    /*
    foreach ( string file in files )
    {
      try
      {
        // Perform whatever action is required in your scenario.
        System.IO.FileInfo fi = new System.IO.FileInfo( file );
        Console.WriteLine( "{0}: {1}, {2}", fi.Name, fi.Length, fi.CreationTime );
      }
      catch ( System.IO.FileNotFoundException e )
      {
        // If file was deleted by a separate application
        //  or thread since the call to TraverseTree()
        // then just continue.
        MessageBox.Show( "Error: " +  e.Message );
        continue;
      }
    } 
    */

    // Push the subdirectories onto the stack for traversal.
    // This could also be done before handing the files.
    foreach ( string str in subDirs )
      dirs.Push( str );

    foreach ( string str in files )
      MessageBox.Show( str );
  }
  • 1 1 Answer
  • 1 View
  • 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-05-14T15:20:14+00:00Added an answer on May 14, 2026 at 3:20 pm

    I got it working using code based on http://weblogs.asp.net/israelio/archive/2004/06/23/162913.aspx

    // How much deep to scan. (of course you can also pass it to the method)
    const int HowDeepToScan=20;
    
    public static void ProcessDir ( string dirName, int recursionLvl, string strFileName)
    {
    
      string tabs = new String( '-', recursionLvl );
    
      if ( recursionLvl<=HowDeepToScan )
      {
        // Process the list of files found in the directory. 
        string [] fileEntries = Directory.GetFiles( dirName );
        TextWriter tw = new StreamWriter( strFileName, true );
        tw.WriteLine( tabs + "<a href=\" " +  System.IO.Path.GetFullPath( dirName ) + "\">" + System.IO.Path.GetFileName( dirName ) + "</a><br />" );
        foreach ( string fileName in fileEntries )
        {
          // do something with fileName
    
          tw.WriteLine( tabs + "<a href=\" " +  System.IO.Path.GetFullPath( fileName ) + "\">" + System.IO.Path.GetFileName( fileName ) + "</a><br />" );
    
        }
        tw.Close();
    
        // Recurse into subdirectories of this directory.
        string [] subdirEntries = Directory.GetDirectories( dirName );
        foreach ( string subdir in subdirEntries )
          // Do not iterate through reparse points
          if ( ( File.GetAttributes( subdir ) &
            FileAttributes.ReparsePoint ) !=
                FileAttributes.ReparsePoint )
    
            ProcessDir( subdir, recursionLvl+1, strFileName );
    
      }
    }
    

    output

    <a href=" C:\code">code</a><br />
    <a href=" C:\code\group.zip">FluentPath (1).zip</a><br />
    <a href=" C:\code\index.html">index.html</a><br />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with some binary data that I have stored in arbitrarily long arrays
I have some imported csv data that I have turned into an xts object.
My goal is to have a form where I fill in some details and
I have an entity Tag (in core data) that have some attributes and a
I have some data (text files) that is formatted in the most uneven manner
I'm using acts_as_taggable_on, but now have a new requirement: The end goal: Users should
having some issues with a networking assignment. End goal is to have a C
can I load a ps1 file from within a ps1 file. The end goal
I have three mysql tables that I would like to extract some information from,
My end goal is to have Selenium running 'within' Jenkins. My Jenkins installation runs

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.