Mahesh Gupta
Mahesh Gupta

Reputation: 1

How to store and retrieve multidimensional data via collection in c#

i need to store and access data like.

[0]   -   
          [0] "some value string7", "some value string 1";
          [1] "some value string9", "some value string 2";

[1]   -   
          [0] "some value strin78", "some value string 34";
          [1] "some value string12", "some value string 56";
          [2] "some value string78", "some value string 12";

[2]   -   [0] "some value string128", "some value string 4";

[3]   -   [0] "some value string65", "some value string 14";
          [1] "some value string85", "some value string 65";
          [2] "some value string95", "some value string 67";
          [3] "some value string13", "some value string 87"

basically, I am making a rename program by windows form which has various types of rename schemes, Its numbers vary all times. I want to create undo operation for many steps. I am using drag and drop for getting files names and locations. I have done it for one step by using two lists. But I am not able to do this more than one step. The above scene should come in each operation of rename, I don't know which variable I should use for it.

I'm using this code for one step 
 for (int i = 0; i < item_Name_New.Count; i++)
 {
     File.Move(item_Name_New[i], item_Name_Old[i]);
 }

item_Name_New & item_Name_Old both are lists which have old & new names of the last operation of rename scheme

Upvotes: 0

Views: 106

Answers (2)

BionicCode
BionicCode

Reputation: 29028

You can use a List that contains Dictionary items where the key is the new file path and the value the old file path:

var fileInfos = new List<Dictionary<string, string>>();

var newNameOldNamePairs1 = new Dictionary<string, string>() 
{ 
  { "C:\New Name 7.txt", "C:\Old Name 1.txt" },
  { "C:\New Name 9.txt", "C:\Old Name 2.txt" }
}

fileInfos.Add(newNameOldNamePairs1);

var newNameOldNamePairs2 = new Dictionary<string, string>();
newNameOldNamePairs2.Add("C:\New Name 78.txt", "C:\Old Name 34.txt");
newNameOldNamePairs2.Add("C:\New Name 12.txt", "C:\Old Name 56.txt");
newNameOldNamePairs2.Add("C:\New Name 78.txt", "C:\Old Name 12.txt");

fileInfos.Add(newNameOldNamePairs2);


// Iterate over the collection of key value pairs to rename the files
foreach (var entry in fileInfos.SelectMany(dictionary => dictionary))
{
  File.Move(entry.Key, entry.Value);           
}

Upvotes: 0

Jann Westermann
Jann Westermann

Reputation: 401

Option 1

Create a class for storing new and old file name and use a nested list:

public class FileAction
{
  public string Old { get; set; }
  public string New { get; set; }
}

Usage:

// A step (item of list) consists of one to many rename actions
List<List<FileAction>> steps = new List<List<FileAction>>();

// ... fill list ...

// Handle steps
foreach (var step in steps)
{
  foreach (var action in step)
  {
    File.Move(action.Old, action.New);
  }
}

Adding a step to the list:

// Get Actions
var actions = new List<FileAction>
{
  new FileAction { Old = "old-file.txt", New = "new-file.txt" },
  new FileAction { Old = "old-file.js", New = "new-file.js" }
};

// Add to steps
steps.Add(actions);

Option 2

Create a class for storing the actions and create a wrapping Step class

public class FileAction
{
  public string Old { get; set; }
  public string New { get; set; }
}

public class Step
{
  public List<FileAction> Actions { get; set; }
}

Usage:

List<Step> steps = new List<Step>();

// ... fill list ...

// Handle steps
foreach (var step in steps)
{
  foreach (var action in step.Actions)
  {
    File.Move(action.Old, action.New);
  }
}

Adding a step to the list:

// Get Actions
var actions = new List<FileAction>
{
  new FileAction { Old = "old-file.txt", New = "new-file.txt" },
  new FileAction { Old = "old-file.js", New = "new-file.js" }
};

// Create and add step
var step = new Step { Actions = actions };
steps.Add(step);

Upvotes: 1

Related Questions