I would like to have some sort of list that I can access by name, and also itterate through.
I have a bunch of subdirectory's that I want to create, like this:
List<string> names = new List<string>();
foreach (var name in names)
{
Directory.CreateDirectory(name);
}
But, I also want to easily access the individual names, like this:
Directory.CreateDirectory(Path.Combine(names.subdir1, "Something"));
I thought of ValueTuples: you can name the fields in a Tuple, but you cannot itterate through a tuple in any way, I think.
Or is a dictionary the best way somehow?
Dictionary<string, string> names = new Dictionary<string, string>();
foreach (var name in names)
{
Directory.CreateDirectory(name.Value);
}
if (names.TryGetValue("subdir1", out string value))
{
Directory.CreateDirectory(Path.Combine(value, "Something"));
}
Is there a better way? Thanks!
Update:
How about if I use a new class:
public class SubDirNames
{
public SubDirNames()
{
}
public const string subdir1 = "Something";
public const string subdir2 = "Something Else";
}
SubDirNames names = new SubDirNames();
string temp = names.subdir1;
Is there a way to itterate through these fields in a class? And still have them also accessible through the names subdir1 and subdir2?
Update 2:
Ok, I will give it one more try to clarify what I like to do: I have to create around 20 subfolders. I only want to define the names of these subfolders once in my program. So this is a nice way to do this:
List<string> namesOfSubFolders = new List<string>
{
"Datasets",
"Docs",
"Manual",
"Visu1",
"Visu2"
};
string projectPath = @"C:\Project";
string workingDir = Directory.GetCurrentDirectory();
foreach (var subFolder in namesOfSubFolders)
{
Directory.CreateDirectory(Path.Combine(projectPath, subFolder));
}
Now for some of these subfolders, I have to do some additional actions. For example: I need to copy some files in the subfolder "Docs". I can think of 2 ways to do this, both are not very nice:
File.Copy(Path.Combine(workingDir, "file.txt"), Path.Combine(projectPath, "Docs"));
File.Copy(Path.Combine(workingDir, "file.txt"), Path.Combine(projectPath, namesOfSubFolders[1]));
In the first line I am repeating the name of the subfolder, and in the second line I have no idea what subfolder I am copying in. Is there another way?