Combining 2 string arrays

T

tshad

I am looking for a way to combine 2 string arrays.

I am trying to get a list of files from 2 directories and combine them:

string[] strFiles;
string[] strFiles2;

strFiles = Directory.GetFiles(Settings.ArchiveFilePath, "*.*");
strFiles2 = Directory.GetFiles(Settings.ExceptionFilePath,
"*.*");

One way I was looking at was to copy them into a string collection:

List<string> sarray = new List<string>();

How would I get the string arrays into the collection? Do I need to do 2
loops and move them in one at a time?

Thanks,

Tom
 
A

Alun Harford

tshad said:
I am looking for a way to combine 2 string arrays.

I am trying to get a list of files from 2 directories and combine them:

string[] strFiles;
string[] strFiles2;

strFiles = Directory.GetFiles(Settings.ArchiveFilePath, "*.*");
strFiles2 = Directory.GetFiles(Settings.ExceptionFilePath,
"*.*");

One way I was looking at was to copy them into a string collection:

List<string> sarray = new List<string>();

How would I get the string arrays into the collection? Do I need to do 2
loops and move them in one at a time?

List<string> sarray = strFiles.Concat(strFiles2).ToList();

Alun Harford
 
T

tshad

Both look pretty good.

Thanks,

Tom

Alun Harford said:
tshad said:
I am looking for a way to combine 2 string arrays.

I am trying to get a list of files from 2 directories and combine them:

string[] strFiles;
string[] strFiles2;

strFiles = Directory.GetFiles(Settings.ArchiveFilePath,
"*.*");
strFiles2 = Directory.GetFiles(Settings.ExceptionFilePath,
"*.*");

One way I was looking at was to copy them into a string collection:

List<string> sarray = new List<string>();

How would I get the string arrays into the collection? Do I need to do 2
loops and move them in one at a time?

List<string> sarray = strFiles.Concat(strFiles2).ToList();

Alun Harford
 
T

tshad

Spoke a little too soon.

Apparently, strFiles.Concat(...) isn't valid.

Do I need another using statement other than using System?

Thanks,

Tom

tshad said:
Both look pretty good.

Thanks,

Tom

Alun Harford said:
tshad said:
I am looking for a way to combine 2 string arrays.

I am trying to get a list of files from 2 directories and combine them:

string[] strFiles;
string[] strFiles2;

strFiles = Directory.GetFiles(Settings.ArchiveFilePath,
"*.*");
strFiles2 = Directory.GetFiles(Settings.ExceptionFilePath,
"*.*");

One way I was looking at was to copy them into a string collection:

List<string> sarray = new List<string>();

How would I get the string arrays into the collection? Do I need to do
2 loops and move them in one at a time?

List<string> sarray = strFiles.Concat(strFiles2).ToList();

Alun Harford
 
A

Alun Harford

tshad said:
Spoke a little too soon.

Apparently, strFiles.Concat(...) isn't valid.

Do I need another using statement other than using System?

You need C# 3 for that one, and

using System.Linq;

Alun Harford
 
P

Pavel Minaev

I am looking for a way to combine 2 string arrays.

I am trying to get a list of files from 2 directories and combine them:

            string[] strFiles;
            string[] strFiles2;

            strFiles = Directory.GetFiles(Settings.ArchiveFilePath, "*.*");
            strFiles2 = Directory.GetFiles(Settings.ExceptionFilePath,
"*.*");

One way I was looking at was to copy them into a string collection:

            List<string> sarray = new List<string>();

How would I get the string arrays into the collection?  Do I need to do2
loops and move them in one at a time?

If you're okay with having array as a result, here's another one:

string[] sarray = new string[strFiles.Length + strFiles2.Length];
strFiles.CopyTo(sarray, 0);
strFiles2.CopyTo(sarray, strFiles.Length);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top