How to do it...

  1. A really nice way to do this is to use an extension method. This way, you can call the extension method directly on the filename variable and have it validated. In your console application, start off by adding a new class called CustomRegexHelper with public static modifier:
        public static class CustomRegexHelper 
{


}
  1. Add the usual extension method code to the CustomRegexHelper class and call the ValidAcmeCompanyFilename method:
        public static bool ValidAcmeCompanyFilename(this string  value) 
{

}
  1. Inside your ValidAcmeCompanyFilename method, add the following regex. We will explain the makeup of this regex in the How it works... section of this recipe:
        return Regex.IsMatch(value,  $@"^acm[_]{DateTime.Now.Year}[_]
({DateTime.Now.Month}|0[{DateTime.Now.Month}])[_]
({DateTime.Now.Day}|0[{DateTime.Now.Day}])(.txt|.docx|.xlsx)$");
  1. When you completed this, your extension method should look like this:
        public static class CustomRegexHelper 
{
public static bool ValidAcmeCompanyFilename(this String value)
{
return Regex.IsMatch(value, $@"^acm[_]{DateTime.Now.Year}[_]
({DateTime.Now.Month}|0[{DateTime.Now.Month}])[_]
({DateTime.Now.Day}|0[{DateTime.Now.Day}])(.txt|.docx|.xlsx)$");
}
}
  1. Back in the console application, create a method with void return type called DemoExtensionMethod():
        public static void DemoExtensionMethod() 
{

}
  1. Add some output text to show the current date and the valid filename types:
        Console.WriteLine($"Today's date is: {DateTime.Now.Year}-
{DateTime.Now.Month}-{DateTime.Now.Day}");
Console.WriteLine($"The file must match: acm_{DateTime.Now.Year}
_{DateTime.Now.Month}_{DateTime.Now. Day}.txt including
leading month and day zeros");
Console.WriteLine($"The file must match: acm_{DateTime.Now.Year}
_{DateTime.Now.Month}_{DateTime.Now. Day}.docx including
leading month and day zeros");
Console.WriteLine($"The file must match: acm_{DateTime.Now.Year}
_{DateTime.Now.Month}_{DateTime.Now. Day}.xlsx including
leading month and day zeros");
  1. Then, add the filename checking code:
        string filename = "acm_2016_04_10.txt"; 
if (filename.ValidAcmeCompanyFilename())
Console.WriteLine($"{filename} is a valid file name");
else
Console.WriteLine($"{filename} is not a valid file name");

filename = "acm-2016_04_10.txt";
if (filename.ValidAcmeCompanyFilename())
Console.WriteLine($"{filename} is a valid file name");
else
Console.WriteLine($"{filename} is not a valid file name");
  1. You will note that the if statement contains the call to the extension method on the variable that contains the filename:
        filename.ValidAcmeCompanyFilename()
  1. If you have completed this, your method should look like this:
        public static void DemoExtensionMethod() 
{
Console.WriteLine($"Today's date is: {DateTime.Now.Year}-
{DateTime.Now.Month}-{DateTime.Now.Day}");
Console.WriteLine($"The file must match: acm_{DateTime.Now.Year}
_{DateTime.Now.Month}_{DateTime.Now.Day}.txt including leading
month and day zeros");
Console.WriteLine($"The file must match: acm_{DateTime.Now.Year}
_{DateTime.Now.Month}_{DateTime.Now.Day}.docx including leading
month and day zeros");
Console.WriteLine($"The file must match: acm_{DateTime.Now.Year}
_{DateTime.Now.Month}_{DateTime.Now.Day}.xlsx including leading
month and day zeros");

string filename = "acm_2016_04_10.txt";
if (filename.ValidAcmeCompanyFilename())
Console.WriteLine($"{filename} is a valid file name");
else
Console.WriteLine($"{filename} is not a valid file name");

filename = "acm-2016_04_10.txt";
if (filename.ValidAcmeCompanyFilename())
Console.WriteLine($"{filename} is a valid file name");
else
Console.WriteLine($"{filename} is not a valid file name");
}
  1. Going back to the console application, add the following code that simply calls the void method. This is just to simulate the versioning method talked about earlier:
        DemoExtensionMethod();
  1. When you are done, run your console application:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.249.252