Loop through sub-directories

Hi,
I want to loop through the sub-directories of one given folder. How to correct the following problems?

            dir = args[0];
            ...
            try
            {

                string [] dirlvl2Ent = Directory.GetDirectories(dir);
                for (string dirlvl2 in dirlvl2Ent)
                {

                }

Error	1	; expected	F:\App8\Program.cs	50	37	ProcessSWFile
Error	2	; expected	F:\App8\Program.cs	50	40	ProcessSWFile
Error	3	Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement	F:\App8\Program.cs	50	40	ProcessSWFile

Open in new window

LVL 11
HuaMin ChenSystem AnalystAsked:
Who is Participating?
 
it_saigeDeveloperCommented:
You need to use foreach:
            try
            {

                string [] dirlvl2Ent = Directory.GetDirectories(dir,"*",SearchOption.AllDirectories);
                foreach (string dirlvl2 in dirlvl2Ent)
                {

                }

Open in new window

-saige-
0
 
it_saigeDeveloperCommented:
Please provide the method that encompasses line 50.

-saige-
0
 
HuaMin ChenSystem AnalystAuthor Commented:
Many thanks.
I now only get the following problem
            try
            {

                string [] dirlvl2Ent = Directory.GetDirectories(dir,"*",SearchOption.AllDirectories);
                for (string dirlvl2 in dirlvl2Ent)
                {

                }
                ...
Error	1	; expected	F:\App8\Program.cs	51	37	ProcessSWFile
Error	2	; expected	F:\App8\Program.cs	51	40	ProcessSWFile

Open in new window

and "for ..." line is line 51.
0
 
Éric MoreauSenior .Net ConsultantCommented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.