i have problem with my check for illegal extension i really dont know why is that not working here's my code
string[] BlacklistedExtensions = { "exe", "msi", "ps1", "reg", "vb", "vbe", "vbs", "scr", "bat", "cmd", "ws", "wsf", "wsh", "dll", "sys", "js" };
string[] Split = Filename.Split('.');
foreach (string Blacklisted in BlacklistedExtensions)
{
if (Split[Split.Length - 1] == Blacklisted)
{
WebData = "Illegal extension name";
Save = false;
}
}
CodePudding user response:
Why don't you consider using a HashSet for the BlacklistedExtensions. This will ensure you constant complexity when checking if the extension of the requested file is illegal.
And my suggestion would look something like that:
// Initialize this HashSet with all the illegal extension values.
// As a second parameter, you can provide an IEqualityComparer<string> if you want to ignore the letter casing.
HashSet<string> blackListedExtensions = new HashSet<string>();
string extension = Filename.Split('.').LastOrDefault();
if (blackListedExtensions.Contains(extension))
{
// This extension is illegal.
}
CodePudding user response:
try this
string[] blacklistedExtensions = { "exe", "msi", "ps1", "reg", "vb", "vbe", "vbs", "scr", "bat", "cmd", "ws", "wsf", "wsh", "dll", "sys", "js" };
string[] arr = Filename.Split('.');
var ext = arr[arr.Length - 1];
if (blacklistedExtensions.Any(e => e.Contains(ext)))
{
var WebData = "Illegal extension name";
var Save = false;
}
