-1

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?

  • 2
    Yes,a dictionary is the best way – Tim Schmelter Dec 12 '17 at 12:30
  • What is `subdir1` ? Can it be any other value? – Chetan Dec 12 '17 at 12:30
  • 1
    What would the `Key` be in your `Dictionary`? The `Value`? – mjwills Dec 12 '17 at 12:35
  • Dictionary is ok, but then I would have to test each time if a key exists. I would like to have an object with a bunch of fields (so I can use Intellisense), and it also be possible to itterate though the fields. Makes any sense? – Stefan Roelofs Dec 12 '17 at 12:35
  • 1
    Side note: You can iterate throught an item field using `GetType().GetProperties()`. I didn't fully understand the question so this might be over kill or of topics. – Drag and Drop Dec 12 '17 at 12:40
  • I don't think there is any way without reflection, but you can easily implement it https://stackoverflow.com/questions/44007004/convert-valuetuple-to-ienumerable, https://stackoverflow.com/questions/8903950/how-do-i-make-this-class-an-ienumerable – Slai Dec 12 '17 at 12:44
  • @StefanRoelofs: not much, no. Maybe it would help if you would show that class instead of showing only strings. – Tim Schmelter Dec 12 '17 at 12:47
  • @StefanRoelofs: now you have shown a class without (non-static) public fields or properties. Can you provide a better example please? Is an instance of this class the key in the dictionary or the value(maybe multiple per key)? The question is still unclear – Tim Schmelter Dec 12 '17 at 12:53
  • Apologies for my unclear question. I will go with a simple dictionary for my program. But the links by @Slai were very interesting. Will see if I can implement that. Thank you all! – Stefan Roelofs Dec 12 '17 at 13:05

1 Answers1

0

If you insist on names.subdir1 syntax you can try using ExpandoObject (which implements IDictionary<string, object> interface):

  using System.Dynamic;

  ...

  dynamic names = new ExpandoObject();

  names.subdir1 = @"C:\MyDir\MySubDir1";
  names.subdir2 = @"C:\MyDir\MySubDir2";

  Directory.CreateDirectory(Path.Combine(names.subdir1, "Something"));

  // Do we have "subdir3" key?
  if ((names as IDictionary<string, object>).ContainsKey("subdir3")) {
    ...
  }

If names["subdir1"] syntax is OK you can try using a Dictionary<string, string>:

  Dictionary<string, string> names = new Dictionary<string, string>() {
    {"subdir1", @"C:\MyDir\MySubDir1"},
    {"subdir2", @"C:\MyDir\MySubDir2"},
  };

  Directory.CreateDirectory(Path.Combine(names["subdir1"], "Something"));
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215