How to compare a value returned by fileupload to ensure it contains 1 of 4 possible filename values (ASP.NET using C#) ?

zimmer9
zimmer9 used Ask the Experts™
on
I am developing an ASP.NET application using C# and .Net Framework 4.0.

I employ the following statement to retrieve the value returned from a browse by the user:

txtFilename.Text = FileUpload2.PostedFile.FileName;

How would you compare the value contained in txtFilename.Text to ensure that it contains one of the following 4 values keeping in mind that txtFilename.Text is prefaced by a drive:
1) MonthlyChecks.txt 2) ML1.txt 3) OS_Mastfile.txt 4) ReversePositive.txt
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2010
Commented:
You can use Contains()

var values = new string[] {"MonthlyChecks.txt", "ML1.txt", "OS_Mastfile.txt", "ReversePositive.txt"};
var found = false;
foreach(var item in values) {
    if (txtFilename.Text.Contains(item)) {
    found = true;
    break;
  }
}
//evaluate found and do something

Open in new window


You could also use Path functions to get the filename from FileUpload2.PostedFile.FileName and do a direct comparison instead of contains()

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial