2016-04-22 2 views
2

ich den folgenden Code geschrieben haben:Wie zu testen, ob eine Datei eine .NET-Assembly in C# ist

public DataTable GetDotNetAssemblies(string baseDirectory) 
{ 
    DataTable MethodResult = null; 
    try 
    { 
     if (Directory.Exists(baseDirectory)) 
     { 
      List<string> FilePaths = NetworkConnection.GetAllFilesUnderDirectory(baseDirectory); 

      DataTable dt = new DataTable(); 
      dt.Columns.Add("Directory"); 
      dt.Columns.Add("Filename"); 
      dt.Columns.Add("Date modified"); 
      dt.Columns.Add("Bytes"); 
      dt.Columns.Add("User modified"); 

      foreach (string FilePath in FilePaths) 
      { 
       DataRow dr = dt.NewRow(); 

       FileInfo f = new FileInfo(FilePath); 

       List<string> AllowedExtensions = new List<string>(); 
       AllowedExtensions.Add(".exe"); 
       AllowedExtensions.Add(".dll"); 

       if (AllowedExtensions.Contains(f.Extension.ToLower())) 
       { 
        dr["Directory"] = f.Directory; 
        dr["Filename"] = f.Name; 
        dr["Date modified"] = f.LastWriteTime; 
        dr["Bytes"] = f.Length.ToString(); 

        string UserModified = ""; 

        try 
        { 
         UserModified = f.GetAccessControl().GetOwner(typeof(System.Security.Principal.NTAccount)).ToString(); 

        } 
        catch 
        { 
         UserModified = "Unknown"; 

        } 

        dr["User modified"] = UserModified; 

        dt.Rows.Add(dr); 

       } 

      } 

      dt.AcceptChanges(); 

      MethodResult = dt; 

     } 
     else 
     { 
      MessageBox.Show("Unable to connect to directory:\n" + baseDirectory); 

     } 

    } 
    catch (Exception ex) 
    { 
     ex.HandleException(); 
    } 
    return MethodResult; 
} 

ich bereits von Dateierweiterung am Filterung, die Sie durch die Linie sehen:

if (AllowedExtensions.Contains(f.Extension.ToLower())) 

Was ich brauche ist, die Assembly-Dateien weiter zu filtern, indem Sie überprüfen, ob es sich um .Net-Assemblys handelt oder nicht.

Gibt es einen Test, den ich an einer Datei durchführen kann?

Darüber hinaus, wenn es möglich ist herauszufinden, welche Version von. Net CLR in der Assembly verwendet wird, wäre das noch besser.

Antwort

-1

Modifizierte Code

Modifiziert nach .NET Baugruppen-Filter:

public DataTable GetDotNetAssemblies(string baseDirectory) 
    { 
     DataTable MethodResult = null; 
     try 
     { 
      if (Directory.Exists(baseDirectory)) 
      { 
       List<string> FilePaths = NetworkConnection.GetAllFilesUnderDirectory(baseDirectory); 

       DataTable dt = new DataTable(); 
       dt.Columns.Add("Directory"); 
       dt.Columns.Add("Filename"); 
       dt.Columns.Add("Date modified"); 
       dt.Columns.Add("Bytes"); 
       dt.Columns.Add("User modified"); 
       dt.Columns.Add(".Net CLR version"); 

       foreach (string FilePath in FilePaths) 
       { 
        DataRow dr = dt.NewRow(); 

        FileInfo f = new FileInfo(FilePath); 

        List<string> AllowedExtensions = new List<string>(); 
        AllowedExtensions.Add(".exe"); 
        AllowedExtensions.Add(".dll"); 

        bool IsDotNetAssembly = false; 

        try 
        { 
         AssemblyName a = AssemblyName.GetAssemblyName(FilePath); 

         IsDotNetAssembly = true; 

        } catch {} 

        if (AllowedExtensions.Contains(f.Extension.ToLower()) && IsDotNetAssembly) 
        { 
         dr["Directory"] = f.Directory; 
         dr["Filename"] = f.Name; 
         dr["Date modified"] = f.LastWriteTime; 
         dr["Bytes"] = f.Length.ToString(); 

         try 
         { 
          Assembly a = Assembly.GetAssembly(AssemblyName.GetAssemblyName(FilePath).GetType()); 

          dr[".Net CLR version"] = a.ImageRuntimeVersion.ToString(); 

         } 
         catch //(Exception ex2) 
         { 
          //ex2.HandleException(); 
         } 

         string UserModified = ""; 

         try 
         { 
          UserModified = f.GetAccessControl().GetOwner(typeof(System.Security.Principal.NTAccount)).ToString(); 

         } 
         catch 
         { 
          UserModified = "Unknown"; 

         } 

         dr["User modified"] = UserModified; 

         dt.Rows.Add(dr); 

        } 

       } 

       dt.AcceptChanges(); 

       MethodResult = dt; 

      } 
      else 
      { 
       MessageBox.Show("Unable to connect to directory:\n" + baseDirectory); 

      } 

     } 
     catch //(Exception ex) 
     { 
      //ex.HandleException(); 
     } 
     return MethodResult; 
    } 

Test 1

eine leicht modifizierte Version des C# Beispiel here gefunden.

Eine Ausnahme wird ausgelöst, wenn es sich bei der Datei nicht um eine .Net-Assembly handelt.

bool IsDotNetAssembly = false; 

try 
{ 
    AssemblyName a = AssemblyName.GetAssemblyName(FilePath); 

    IsDotNetAssembly = true; 

} catch {} 

-Test 2

Teilen Sie die Version der CLR, die die .NET-Compiler-Version ist:

Assembly a = Assembly.GetAssembly(AssemblyName.GetAssemblyName(FilePath).GetType()); 

dr[".Net CLR version"] = a.ImageRuntimeVersion.ToString(); 
3

könnten Sie versuchen, die GetAssemblyName() Methode, um zu versuchen zu lösen, wenn es eine tatsächliche Anordnung ähnlich wie the technique mentioned in this MSDN documentation war:

public string bool IsValidAssembly(string path) 
{ 
    try 
    { 
      // Attempt to resolve the assembly 
      var assembly = GetAssemblyName(path); 
      // Nothing blew up, so it's an assembly 
      return true; 
    } 
    catch(Exception ex) 
    { 
      // Something went wrong, it is not an assembly (specifically a 
      // BadImageFormatException will be thrown if it could be found 
      // but it was NOT a valid assembly 
      return false; 
    } 
} 

So könnte man erwägen die folgenden in Ihrem Code verwenden:

// Check that it is a valid extension and a valid assembly 
if (AllowedExtensions.Contains(f.Extension.ToLower()) && IsValidAssembly(FilePath)) 
{ 
    // At this point it should be valid, so continue 
} 
Verwandte Themen