Turn comma-separated string into GetFiles SearchPattern

Dumpen

I have the following code:

private string[] FindExistingDocuments()
{
    string supportedImageFormats = "jpg,pdf,doc,docx,xlsx";

    DirectoryInfo documentPath = new DirectoryInfo("...");

    string supportedFileTypes = String.Join(",*.", supportedImageFormats.Split(','));
    string[] files = Directory.GetFiles(documentPath.FullName, supportedFileTypes, SearchOption.AllDirectories);

    return files;
}

Which functions as a way of searching for a specific list of file types but the problem with the current code is that String.Join does not put the separator at the first item (which makes sense).

So my supportedFileTypes turns out to be:

jpg,*.pdf,*.doc,*.docx,*.xlsx

But I want it to be:

*.jpg,*.pdf,*.doc,*.docx,*.xlsx

Can I somehow make this in a pretty clean way?

Note: I cannot alter the contents of supportedImageFormats

Habib
string newStr = string.Join(",", supportedImageFormats.Split(',')
                                     .Select(r => "*." + r));

Output: Console.WriteLine(newStr);

*.jpg,*.pdf,*.doc,*.docx,*.xlsx

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Turn comma-separated string into GetFiles SearchPattern

From Dev

Sum of comma separated string

From Dev

Read string separated by comma

From Dev

Comma Separated String

From Dev

Read string separated by comma

From Dev

Array to comma separated string?

From Dev

Handle comma separated string

From Dev

Turn properties of object into a comma-separated list?

From Dev

Turn a value range in a cell into a comma separated list

From Dev

Turn a value range in a cell into a comma separated list

From Dev

Turn a php array into comma separated array

From Dev

Easy way to turn JavaScript array into comma AND Quotation Marks separated string list?

From Dev

IEnumerable<string> to comma separated string

From Dev

IEnumerable<string> to comma separated string

From Dev

searchPattern Logic on Directory.GetFiles Method

From Dev

Directory.GetFiles searchPattern inconsistent with documentation

From Dev

searchPattern Logic on Directory.GetFiles Method

From Dev

Split a comma separated string but only split by a comma

From Dev

Regular expression for a comma separated string

From Dev

Convert comma separated string into a HashSet

From Dev

Convert comma separated string to a list

From Dev

comma separated string to list in r

From Dev

Convert comma separated string to datetime

From Dev

Pass a comma separated string as a list

From Dev

Reformat string to be comma separated and formatted

From Dev

comma separated string using CTE

From Dev

PHP array comma separated string

From Dev

Convert comma separated string to datetime

From Dev

Arduino String separated by comma to Array