How change assembly information of copied file to a new location

2.1k views Asked by At

i want to change AssemblyInfo of copied executeable file to a new location and keep the main information of main executeable file safe.
i am using the codes below to copy my file to new location with new file name.

        String fileDestination = Path.Combine(Environment.GetFolderPath(folder), "settings.exe");

        if (!File.Exists(fileDestination))
            File.Copy(Application.ExecutablePath, fileDestination);

but AssemlyInfo of new file is same as source file and i want to change them.
how can i do that before copying original file?

1

There are 1 answers

1
Nico On BEST ANSWER

MoonLight,

This is possible, however not exactly an easy solution. You have a few options here.

I must note, that to answer your question. It is not possible to change the assembly information during a simple copy. The assembly information is "cooked" into the assembly and not changeable.

Option 1:

If at all possible I would do this before the assemby has been built. Typically this can be done using SharedAssembly information that you can move across your application. In this event you "could" use a pre-build "Event" to copy a SharedAssembly info over to the target directory. The SharedAssembly path is quite easy and is described here. http://blogs.msdn.com/b/jjameson/archive/2009/04/03/shared-assembly-info-in-visual-studio-projects.aspx It uses the process of linked files between your solution projects giving applying all or partial of the assembly info (or all) to your projects. In this case you could use the pre-build event to do an xcopy of the preffered SharedAssembly Information thus linking the new Assembly Information. Now to make this process whole I would do this.

  • Create a Solution Items solution folder.
  • Create a SharedAssemblyInfo.cs file in your solution root and specify your common attributes.
  • Add the SharedAssemblyInfo.cs file to each solution you wish to target.
  • Create a SharedAssembly2.cs file with the new information that you wish to have in the new target.
  • Write your pre-build events to backup your SharedAssemblyInfo.cs file, move the SharedAssebly2.cs to the SharedAssemblyInfo.cs location
  • Write your post-build event to re-instante the SharedAssemblyInfo.cs file. Something like.

Pre-Build.

xcopy /Y $(SolutionDir)SharedAssemblyInfo.cs $(SolutionDir)SharedAssemblyInfo.bak
xcopy /Y $(SolutionDir)SharedAssembly2.cs $(SolutionDir)SharedAssemblyInfo.cs

Post-Build

xcopy /Y $(SolutionDir)SharedAssemblyInfo.bak $(SolutionDir)SharedAssemblyInfo.cs

Option 2:

There is a package from the Mono framework that "could" do this. However this is much more difficult, in addition this package only changes the assembly definition and not the Win32Resources that are visibile when viewing the File properties -> details tab.

Using the Package Mono.Cecil on Nuget this is possible. Here you can change \ add assembly attributes, Names etc.

Here is a sample to change some attributes (note there are many more and best to use your debugger and review the available attributes).

static void Main(string[] args)
{
    if (args.Length != 2)
    {
        Console.Error.WriteLine("Expected arguments: <Assembly-Path> <New-Assembly-Path>");
        Environment.Exit(1);
    }

    var assemblyPath = args[0];
    var newAssemblyPath = args[1];
    var assemblyDef = AssemblyDefinition.ReadAssembly(assemblyPath);

    Console.WriteLine("Loaded assembly " + assemblyDef);

    assemblyDef.Name.Name = "New Name";
    assemblyDef.MainModule.Name = "new Name";
    assemblyDef.Name.Version = new Version(1, 0, 0, 0);

    var resources = assemblyDef.MainModule.Resources;

    var att = assemblyDef.CustomAttributes.FirstOrDefault(x => x.AttributeType.Name == "AssemblyFileVersionAttribute");
    att.ConstructorArguments.Clear();
    att.ConstructorArguments.Add(new CustomAttributeArgument(new TypeReference("System", "string", null, null), "1.0.0.0"));

    assemblyDef.Write(newAssemblyPath);
}

In the end if you really, really want to change the file details it will have to be done at compile time using Option 1.

I hope this helps.