Q:

How to delete all files without deleting a specific file in Asp.net solution explorer's folder?

I am trying this piece of code to delete all files except a particular file whose path+name is hold by a ViewState.
string[] filePaths = Directory.GetFiles(@"E:\Projects\BlockSchemeManagement\Attachment");

foreach (string filePath in filePaths)
         File.Delete(filePath);

This code should delete all files but it is not deleting any of them.
This is the viewState to keep this particular file by the way:
ViewState["file"] = @parentDir + filename;

Can any one give suggestion how to proceed? 

A:

If file is opened and locked by your application or by any other application then you cannot delete it. Try to ignore the exception on first file and delete other files.
string[] filePaths = Directory.GetFiles(@"E:\Projects\BlockSchemeManagement\Attachment");

foreach (string filePath in filePaths) 
{
    if (filePath != ViewState["file"])
    {
        try
        {
             File.Delete(filePath);
        } 
          catch { }
    }
}

Note that ViewState["file"] should have full name of file such as 
E:\Projects\BlockSchemeManagement\Attachment\filename.ext

