Build the File Names in a source directory using C# in 1.1

We can use the System.Collection.Specialized.StringCollection class object to Add a range of values and then use its CopyTo method to obtain the single dimensional array.

public string[] BuildFileList(string SourceLocation, string[] FileExtensions)
        {
            System.Collections.Specialized.StringCollection FileListRequired = new System.Collections.Specialized.StringCollection();
            
            //If it is a File
            if (File.Exists(SourceLocation))
            {
                FileListRequired.Add(SourceLocation);
            }
            else //If it is a Directory
            {
                foreach(string FileExtn in FileExtensions)
                {
                    FileListRequired.AddRange(Directory.GetFiles(SourceLocation,string.Concat("*.",FileExtn)));
                }
            }


            string[] FileList = new string[FileListRequired.Count];
            FileListRequired.CopyTo(FileList, 0);

            return FileList;
        }
By [)ia6l0 iii   Popularity  (559 Views)