Thread: file recursion help

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    731

    file recursion help

    How would I get all sub directories in the base master directory (C drive).

    I also need to use this for the registry (please I know what I am doing)

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Did you know the FAQ is searchable?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    well to bad the faq doesn't have any file recursion for C#.

    I'm sure the c++ code is convertable but I really don't know c++ as good as C#.

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    p.s this is the C board not the C# board
    Last edited by prog-bman; 03-22-2005 at 08:16 PM.
    Woop?

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    can someone move this please, I feel like an idoit.

    please move this to C# board.

    thank you for your patients with me.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    In other words, you can only get away with being a smart ass if you're not a dumb ass.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Moved to C# board as requested.

    However you may want to provide a little bit more detail.

  8. #8
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    I want to collect the names of all of the directorys in the, lets say, C drive. From there I can get the names of the files, and any other info I need.

    That is as clear as I can get it.

  9. #9
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    Quote Originally Posted by Rune Hunter
    I want to collect the names of all of the directorys in the, lets say, C drive. From there I can get the names of the files, and any other info I need.

    That is as clear as I can get it.
    Simple recursion:
    Code:
    using System;
    using System.Collections;
    using System.IO;
    
    namespace GetDirs
    {
    	class entry
    	{
    		static void Main(string[] args)
    		{
    			Test test = new Test();
    			test.GetDirs(@"C:\");
    			test.DumpDirs();
    		}
    	}
    
    	class Test
    	{
    		private ArrayList directories = new ArrayList();
    
    		public void GetDirs(string home)
    		{
    			string[] dirs1 = Directory.GetDirectories(home);
    			foreach (string dir in dirs1)
    			{
    				directories.Add(dir);
    				try
    				{
    					string[] dirs2 = Directory.GetDirectories(dir);
    					if (dirs2.Length > 0)
    						GetDirs(dir);
    				}
    				catch { }
    			}
    		}
    
    		public void DumpDirs()
    		{
    			foreach (object obj in directories)
    				Console.WriteLine((string)obj);
    		}
    	}
    }

  10. #10
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    And here I modified the above example to do the same thing with registry keys:
    Code:
    using System;
    using System.Collections;
    using Microsoft.Win32;
    
    namespace GetReg
    {
    	class entry
    	{
    		static void Main(string[] args)
    		{
    			Test test = new Test();
    			test.GetRegKeys(Registry.ClassesRoot);
    			test.GetRegKeys(Registry.CurrentUser);
    			test.GetRegKeys(Registry.LocalMachine);
    			test.GetRegKeys(Registry.Users);
    			test.GetRegKeys(Registry.CurrentConfig);
    			test.DumpRegKeys();
    		}
    	}
    
    	class Test
    	{
    		private ArrayList regkeys = new ArrayList();
    
    		public void GetRegKeys(RegistryKey homekey)
    		{
    			string[] subkeys = homekey.GetSubKeyNames();
    			foreach (string subkey in subkeys)
    			{
    				try
    				{
    					regkeys.Add(homekey.Name + '\\' + subkey);
    					RegistryKey subkey2 = homekey.OpenSubKey(subkey);
    					if (subkey2.SubKeyCount > 0)
    						GetRegKeys(subkey2);
    					subkey2.Close();
    				}
    				catch { }
    			}
    		}
    
    		public void DumpRegKeys()
    		{
    			foreach (object obj in regkeys)
    				Console.WriteLine((string)obj);
    		}
    	}
    }

  11. #11
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    I don't see where test is coming from? It isn't in Visual C# 2005 at least.


    Edit: nvm I see now.

  12. #12
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    omg thank you so much! I am going to study and learn how this works now!

    1 problem, it says I don't have access to the registry but I am full adminster and have full permisions on this computer. Why can't I with .net?

    Edit: Hmm I guess my program I made before just was bad or somtin... it works fine now.
    Last edited by Rune Hunter; 03-24-2005 at 06:56 PM.

  13. #13
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    ok so how would I get the value of the key it is in as well. I have tried many, many things but I got no results.

  14. #14
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    Quote Originally Posted by Rune Hunter
    ok so how would I get the value of the key it is in as well. I have tried many, many things but I got no results.
    http://msdn.microsoft.com/library/de...namestopic.asp + foreach + http://msdn.microsoft.com/library/de...valuetopic.asp



    Also, do a little search on recursion to understand why it works.

  15. #15
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    alright I should do some research by just by looking at it I can grasp how it works.

    And thank you, I was alot confused on C# registry values. The registry can be confusing at times.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  2. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  3. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM