0

I am attempting to take two string collections and combine each line in both collections simultaneously to show a full file path for my user.

Examples to help clear up confusion:

  • String Collection 1 will contain a list of paths.
    Example:

    C:\windows\xxxx\xxx\xx, C:\Users\xxx\xxx, C:\test\xxx\xxx
    
  • String Collection 2 will contain a list of file names.
    Example:

    file.txt, asd.txt, mydll.dll
    

Each list holds the key to one another in the same line number and I just need to combine them to output them to the end user.

String Collection Line 1 + SC2 Line 1 = Path
SC Line 2 + SC2 Line 2 = Path
SC Line 3 + SC2 Line 3 = Path

The information is not accessible in a combined state so I will be placing the data into two separate WPF TextBoxes and then their content will be pulled into a stringcollection.

First Collection

StringCollection lines = new StringCollection();
int lineCount = filePath.LineCount;

for (int line = 0; line < lineCount; line++)
    // Get line text and add to string collection
    lines.Add(filePath.GetLineText(line));

Second Collection

StringCollection lines2 = new StringCollection();
int lineCount2 = fileName.LineCount;

for (int line = 0; line < lineCount; line++)
    // Get line text and add to string collection
    lines.Add(fileName.GetLineText(line));

Any and all help is appreciated!

Edit 1

I have experimented with the ZIP command thanks to Eve but I also found an alternate route. Is there a safer route using zip compared to the code below?

Keep in mind I will have a function to check the line counts and make sure they are the same.

        StringCollection lines = new StringCollection();
        int lineCount = itemIDBox.LineCount;

        for (int line = 0; line < lineCount; line++)
        {
            string id;
            string rev;
            string combined;

            id = itemIDBox.GetLineText(line);
            rev = revBox.GetLineText(line);

            combined = id + @"\" + rev;

            lines.Add(combined);
        }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jesse
  • 434
  • 2
  • 6
  • 16
  • Instead of Concatenating the string using `+` why not look up the string.Join() method if not then look up the string.Split() method you want the paths you could just do `string[] splitStr = Split(',');` – MethodMan Jan 10 '13 at 22:25
  • Don't do `@"\"`, but instead use `Path.DirectorySeparatorChar`. – myermian Jan 11 '13 at 13:09

1 Answers1

4

You can use the Zip method from System.Linq.

var fullPaths = lines.Cast<string>().
    Zip(lines2.Cast<string>(), (path, fileName) => Path.Combine(path, fileName)).
    ToArray();
e_ne
  • 8,340
  • 32
  • 43
  • 2
    Use [Path.Combine](http://msdn.microsoft.com/en-us/library/dd781134.aspx) instead of `+`. – Mike Jan 10 '13 at 22:31