Nick
Nick

Reputation: 1

Read DLL content programmatically in C#

I am working on a small c# console application that will check my .net DLL and look for environment specific information from compiled dll.

Basically, I want to check c# Web published project and display any file contain production specific information developer forgot to update...

Issue: I am running into is when a developer switch between environment from Dev to test and test to prod. They forget to switch their environment value either in C# or web.config file.

Is there a way I can open individual DLL and extract the DLL content as a string using C# code and a free decompiler?

Upvotes: 0

Views: 3376

Answers (2)

vineet sahu
vineet sahu

Reputation: 67

here is the code to read the information for any .dlls present in your target directory.

code here

  foreach (var file in Directory.EnumerateFiles(dirPath, "*.dll"))
  { var assembly = Assembly.LoadFile(file);
  GetCustomAttributeVlaues(assembly);
  AssemblyName assemblyName = assembly.GetName();
  Console.WriteLine("Assembly Name: " + assemblyName.Name);
  Console.WriteLine("Assembly Version: " + assemblyName.Version);
  Console.WriteLine("Assembly Culture: " + assemblyName.CultureInfo.Name);
  Console.WriteLine("Assembly Public Key Token: " + 
 BitConverter.ToString(assemblyName.GetPublicKeyToken()));
}

private static void GetCustomAttributeVlaues(Assembly assembly)
{
var data = assembly.GetCustomAttributesData(); 
foreach (var item in data)
{
    switch (item.AttributeType.Name)
    {
        case nameof(AssemblyFileVersionAttribute):item.ConstructorArguments.ToList().ForEach(x=>Console.WriteLine(x.Value)); break;
        case nameof(AssemblyCopyrightAttribute): item.ConstructorArguments.ToList().ForEach(x => Console.WriteLine(x.Value)); break;
        case nameof(AssemblyCompanyAttribute): 
             item.ConstructorArguments.ToList().ForEach(x => 
             Console.WriteLine(x.Value)); break;
        default:
            break;
    }
       }

}

Upvotes: 0

Jorge Aguiar
Jorge Aguiar

Reputation: 61

Try this:

using System;
using System.Linq;
using System.Reflection;

#if DEBUG
[assembly:AssemblyConfiguration("debug")]
#else
[assembly:AssemblyConfiguration("release")]
#endif

namespace ConsoleApp1
{
    internal class Program
    {
        private static void Main()
        {
            // this should be the filename of your DLL
            var filename = typeof(Program).Assembly.Location;

            var assembly = Assembly.LoadFile(filename);
            var data = assembly.CustomAttributes.FirstOrDefault(a => a.AttributeType == typeof(AssemblyConfigurationAttribute));
            if (data != null)
            {
                // this will be the argument to AssemblyConfigurationAttribute
                var arg = data.ConstructorArguments.First().Value.ToString();
                Console.WriteLine(arg);
            }
        }
    }   
}

Upvotes: 0

Related Questions