I have a C# application that needs to recognize whether a given string is a valid Windows file path that ends in a file name with extension. Currently I have:
public static bool IsValidWindowsFilePathWithFileName(string filepath)
{
string scriptFilePathPattern = @"^[A-Za-z]:(\\\w+(\s*\w+)*)*(\\\w+(\s*\w+)*\.[a-zA-Z]\w*)$";
return Regex.IsMatch(filepath, scriptFilePathPattern);
}
This normally works fine. However, if I put in something that ends with a long string, such as
"C:\Windows\System32\ThisIsALongBadFileName"
it takes several minutes to process. Is there a better way to do this?