Home > Software design >  How to validate multiple filenames inside string array in c#
How to validate multiple filenames inside string array in c#

Time:03-30

Inside a string array -"files", 10 file paths are stored. For example, I show 2 of them,

[0] C:\\Users\\17\\Documents\\FS\\D\\mdN_2903.dat
[1] C:\\Users\\17\\Documents\\FS\\D\\mdNBP_29032.dat

I want to validate using their filename (before the numbering & extension .dat) as well the number of files counted in c#.net

I tried to use it like this, but it didn't passed even though the files are present

if(Array.Exists(files,e => e.Contains("mdN_") && e.Contains("mdNBP_") && e.count()==10)) { // All 10 files are present }

Is there any other way to implement this in c#, please explain?

CodePudding user response:

Without testing it. If your file array always has 2 has mdN and mdNBP and the same order with up to 10 files, you can do something like this:

if (files[0].Contains("mdN") && files[1].Contains("mdNBP") && files.Length == 10)
{
    Console.WriteLine("all good");
}

You can also use linq just example, it can be done differnt ways, here the order does not matter, it just check if you files contian the mentioned values:

if (files.Any(e => e.Contains("mdN") && e.Contains("mdNBP") && files.Length == 10))
{
    Console.WriteLine("all good");
}

If you have more files, then you can for loop the file array and check against a list of what it should contain.

  • Related