Nick Kahn
Nick Kahn

Reputation: 20078

optimized this code and make it more dynamic

is there a way i can optimized this code and make this extensions on web.config settings so i can read from there and in future if i need to add or remove i should be able to do easily?

  if (fileExt.ToLower() == ".rtf" || 
                    fileExt.ToLower() == ".xls" || 
                    fileExt.ToLower() == ".doc" || 
                    fileExt.ToLower() == ".png" || 
                    fileExt.ToLower() == ".gif" || 
                    fileExt.ToLower() == ".txt" || 
                    fileExt.ToLower() == ".jpg" || 
                    fileExt.ToLower() == ".pdf" || 
                    fileExt.ToLower() == ".htm" || 
                    fileExt.ToLower() == ".html" ||
                    fileExt.ToLower() == ".rar" || 
                    fileExt.ToLower() == ".zip")
                 {

Upvotes: 1

Views: 178

Answers (5)

dkackman
dkackman

Reputation: 15579

    string[] yourarray = { ".rtf", ".xls",".doc",".png",".gif",".txt",
                 ".jpg",".pdf",".htm",".html",".rar",".zip" };


    if(yourarray.Contains(fileExt, StringComparer.OrdinalIgnoreCase))
    {
        //
    }

Contains is a Linq extension. If performance is a concern you could use a Dictionary and its ContainsKey which will do a hash lookup not an iteration.

Upvotes: 0

Josh Stodola
Josh Stodola

Reputation: 82523

In your web.config...

<appSettings>
  <add key="ValidExtensions" value="rtf,xls,doc,png,gif,txt,jpg,pdf" />
</appSettings>

In your code-behind...

fileExt = fileExt.ToLower().TrimStart(".")

Dim ConfigSetting As String = ConfigurationManager.AppSettings("ValidExtensions")
Dim Extensions As String() = ConfigSetting.Split(",")
Dim IsValid As Boolean = (Array.IndexOf(Extensions, fileExt) >= 0)

If IsValid Then
    'Yada, yada, yada
Else
    'Error
End If

Upvotes: 0

AndreyAkinshin
AndreyAkinshin

Reputation: 19061

string[] extensions = { ".rtf", ".xls", ".doc", ".png", ".gif", ".txt",
                        ".jpg", ".pdf", ".htm", ".html", ".rar", ".zip" };

if (extensions.Contains(fileExt.ToLower())){
}

All IEnumerable<string> have extension-method Contains(string value). You can use any IEnumerable<string> instead of string[] in my example.

Upvotes: 0

george9170
george9170

Reputation:

If you want to use the web.config I would recommend using custom configuration sections. This way you can enumerate over the strongly typed results which come back. link below

custom configuration sections

you can use a hashtable to store the values and just do a search in that, it will be faster then an array or a list. and the code will look something like this

pseudoe code below, you will have to check if the variable is in the hash table.

  Hashtable fileExtsions = new Hashtable();
if (fileExtensions[extensionOfFileInquestion])
{
do some code
}

so all you have to do is check if the extension of the file is in the hashtable. that will limit your if statement and is faster then an array. You can populate the hash table with the custom config section enumerable.

the downside is the hash table will take more memory then the string of arrays but in the end you might be happier

hash tables

Upvotes: 1

Emmaneale Mendu
Emmaneale Mendu

Reputation: 1

You can use list:

string[] yourarray = { ".rtf", ".xls",".doc",".png",".gif",".txt",
                         ".jpg",".pdf",".htm",".html",".rar",".zip" };

var foundedString = Array.Find(yourarray, str => str.ToLower().Equals(fileExt));

if (foundedString == null)
{
    //Not Found
}
else
{
    //Code here
}

Upvotes: 0

Related Questions