如何在C#中测试文件是否为.Net程序集

Kni*_*ins 4 .net c# datatable file fileinfo

我写了以下代码:

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;
}
Run Code Online (Sandbox Code Playgroud)

我已经按文件扩展名过滤了,您可以通过以下行看到它:

if (AllowedExtensions.Contains(f.Extension.ToLower()))
Run Code Online (Sandbox Code Playgroud)

我需要的是进一步检查程序集文件,方法是检查它们是否为.Net程序集。

我可以对文件执行测试吗?

另外,如果有可能发现程序集中使用了哪个版本的.Net CLR,那么效果会更好。

Rio*_*ams 6

您可以尝试使用该GetAssemblyName()方法来尝试解决该方法是否是类似于本MSDN文档中提到的技术的实际程序集:

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;
    }   
}
Run Code Online (Sandbox Code Playgroud)

因此,您可以考虑在代码中使用以下内容:

// 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
}
Run Code Online (Sandbox Code Playgroud)