Last active
October 3, 2019 21:14
-
-
Save kyrathasoft/1dc1a1eb4d6a2a56827dcb5f43840afe to your computer and use it in GitHub Desktop.
String array containing top-level subdirectories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* shows how to get a string array holding the names of all top-level subdirectories | |
online at: | |
https://gist.github.com/kyrathasoft/1dc1a1eb4d6a2a56827dcb5f43840afe | |
To compile: csc *.cs | |
Here is the structure of this example program | |
and associated directories, on disk: | |
examples/CountTopLevelDirectories | |
- list.cs | |
- boo | |
- coo | |
- chirp | |
squawk | |
- loo | |
- who | |
*/ | |
using System; | |
using System.IO; | |
namespace Example { | |
class Program { | |
static void Main(string[] args){ | |
if(Directory.Exists("boo")){ | |
string[] dirs = Directory.GetDirectories(@"boo", "*", SearchOption.TopDirectoryOnly); | |
Console.WriteLine(" # of top-level subdirectories found within directory 'boo': {0}", | |
dirs.Length); | |
if(dirs.Length > 0){ | |
Console.WriteLine(" List of the names of those top-level subdirectories:"); | |
for(int i=0; i < dirs.Length; i++){ | |
string subdir = dirs[i]; | |
if(subdir.StartsWith("boo\\")){ subdir = subdir.Substring(4,subdir.Length -4); } | |
Console.WriteLine("\t{0}", subdir); | |
} | |
} | |
}else{ | |
Console.WriteLine("\n Sorry. This program requires a directory named 'boo'..."); | |
PressToExit(); | |
} | |
} | |
static void PressToExit(){ | |
Console.Write(" Press any key to exit... "); | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment