2016-04-22 34 views
2

我寫了下面的代碼:如何測試一個文件是否是一個.NET程序集在C#

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; 
} 

我已經通過文件擴展名,您可以通過行看到過濾:

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

我需要的是通過檢查它們是否是.Net程序集來進一步過濾程序集文件。

我可以在文件上執行測試嗎?

此外,如果有可能發現在程序集中使用.Net CLR的哪個版本,那麼這樣會更好。

回答

-1

修改後的代碼

改性過濾的.Net組件:

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; 
    } 

試驗1

A中的C#示例的略加修改的版本發現here

如果文件不是.Net程序集,則會拋出異常。

bool IsDotNetAssembly = false; 

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

    IsDotNetAssembly = true; 

} catch {} 

測試2

告訴CLR您的版本,這是.NET編譯器版本:

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

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

你可以嘗試使用GetAssemblyName()法來解決,如果它是the technique mentioned in this MSDN documentation類似的實際裝配:

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; 
    } 
} 

所以,你可以考慮使用你的代碼如下:

// 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 
} 
相關問題