Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
string[] filesHD = Directory.GetFiles(dirPath, filepattern1);
string[] filesDT = Directory.GetFiles(dirPath, filepattern2);
string[] filesTD = Directory.GetFiles(dirPath, filepattern3);

Here filesHD[] Array contains 2 files. filesDT[] contains 2 files and filesTD[] also contains 2 files. I want to create a single string Array which will contain all the 6 files of filesHD,filesDT,filesTD.

string[]Allfiles=new string [filesHD+filesDT+filesTD].

Please help me to do this.
Posted
Updated 16-Jan-14 6:22am
v2
Comments
Sergey Alexandrovich Kryukov 16-Jan-14 12:23pm    
What have you tried so far?
—SA
DEbopm 16-Jan-14 12:27pm    
i tried like this ..
string[]Allfiles=new string [filesHD.length+filesDT.length+filesTD.length].
Sergey Alexandrovich Kryukov 16-Jan-14 12:31pm    
Isn't it obvious that it does not work? Who told you that the operation + is defined for arrays? Array objects are immutable.
Please see my complete answer, Solution 1.
—SA

The main question should be: why putting them all in a single array?
Also, arrays objects are immutable (even though their elements are mutable), which makes them not very suitable for holding variable-length data; for this purpose, you should better use some collection from System.Collections.Generic, like System.Collections.Generic.List<string>:
http://msdn.microsoft.com/en-us/library/System.Collections.Generic%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx[^].

You can always get an array from the list using the method ToArray, if you need to.

If you still want to create the array immediately from other array(s) (not recommended for most cases), just use Array.Copy:
http://msdn.microsoft.com/en-us/library/system.array.copy%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
v2
Comments
Maciej Los 17-Jan-14 19:27pm    
It's late now, and i'm a bit tired to explain the reasons i disagree with some parts of your answer. But, you need to remember that i agree with your answer in general...
Please, see my answer.
Maciej Los 18-Jan-14 7:37am    
Upvoted!
Sergey Alexandrovich Kryukov 18-Jan-14 18:39pm    
Thank you, Maciej. Stay getting enough sleep/relaxation and alert. :-)
—SA
Linq[^] would be helpful in this case.

I would suggest to use Union[^] statement.

C#
string[] filesHD = {"file1.hd","file2.hd"};
string[] filesDT= {"file1.dt"," file2.dt"};
string[] filesTD = {"file1.td","file2.td"};

var allfiles=filesHD.Union(filesDT).Union(filesTD);

foreach(string file in allfiles) 
{
    Console.WriteLine("file: {0}", file);
}


Note: not tested!
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 17-Jan-14 21:51pm    
Whatever you do with arrays, they are not immutable (only their elements are), so you always create a brand-new array instance each time, which can be quite inefficient if repeat it, as in your example. With what point do you disagree (this message will server as a reminder for you).

And this Union is just a higher level of Copy. Anyway, this is easier to do without the bugs, so my 5. The real solution would be not creating arrays in first place, if creation of arrays of different size will be later needed. Collections should be used instead.

—SA
Maciej Los 18-Jan-14 7:31am    
Thank you, Sergey.
By The Way: I have read your answer one more time and i need to admit that i was really tired... +5!
Try this..


C#
List<string> lstTemp = new List<string>();
           lstTemp.AddRange(filesHD);
           lstTemp.AddRange(filesDT);
           lstTemp.AddRange(filesTD);
           string[] finalArray = lstTemp.ToArray();


note: pls add this namespace using System.Collections.Generic;

Reference:List AddRange[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900