views:

73

answers:

5

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")
                 {
A: 

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
}
Emmaneale Mendu
@Emmaneale: you meant "array", not "list", right? I don't see any lists here.
John Saunders
+1  A: 

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

george9170
A: 
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.

DreamWalker
A: 

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
Josh Stodola
Wow, StackOverflow does a lovely job of syntax highlighting on VB.NET, don't you think?
Josh Stodola
No big deal, but you might want to use a different delimiter; one that is not a valid character in a file name, such as "|". Just in case some crazy person actually wants to have a file extension with a comma in it. `<add key="ValidExtensions" value="rtf|xls|doc|png|gif|txt|jpg|pdf" />` (Unless you're running on Mono, I suppose, then all bets are off!)
Dr. Wily's Apprentice
If the file in question is being uploaded by a user, filenames that are illegal under Windows become a valid concern. I've had ASP.NET code crash because a Mac user uploaded a file with a colon in the name and I tried to save the file to same filename.
Carson63000
A: 
    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.

dkackman