1

Basically I would like to set AssemblyTitle in my constructor, the reason of doing this is because I have several versions of program and they all sharing same AssemblyInfo.cs

I am looking for something like that

AssemblyTitleAttribute title = "Full Version";

but this one doesn't work, any ideas?

inside
  • 3,047
  • 10
  • 49
  • 75

4 Answers4

3

The attribute is a complie time constant so it cannot be "In the constructor", you have two options:

  1. A class that is unique to the full version has [assembly: AssemblyTitle("Sandbox Console")] (put it before you declare the namespace)
  2. Use a #if in the AssemblyInfo.cs to choose the name and set up your build environment to define the directive in your Conditional Compilation Symbols in the Build tab of Properties.
// AssemblyInfo.cs

//(snip)

[assembly: AssemblyCopyright("Copyright ©  2011")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
#if FULL_VERSION
    [assembly: AssemblyTitle("XYZ App (Full Version)")]
#else
    [assembly: AssemblyTitle("XYZ App (Trial Version)")]
#endif

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • I need to execute some functions before I can set assembly title, already tried that, but not an option for me... – inside Jan 09 '13 at 20:54
  • Are you talking about the title that shows up in the window of the program or the title that shows up when you right click and go to properties? – Scott Chamberlain Jan 09 '13 at 20:56
  • That is not `AssemblyTitle`, Just `this.Text = "Blah";` from the form's constructor [sets that title](http://msdn.microsoft.com/en-us/library/ms159419%28v=vs.90%29.aspx). `AssemblyTitle` is for when you right click on a EXE and go to properties. – Scott Chamberlain Jan 09 '13 at 21:15
  • Already changed it, for some reason program used AssemblyTitle as window title... Thanks – inside Jan 09 '13 at 21:23
  • any chance I can stuff a define constant from the csproj into the Assembly Title? – tofutim Sep 21 '16 at 23:46
  • @ScottChamberlain In your example say I set 'FULL_VERSION' in the the conditional compilation symbols of the build properties -is it possible to use this with different build configurations i.e. set Trials configuration to have one assembly title, but the full configuration compiles it to a different title. – erotavlas Apr 01 '21 at 19:56
1

You can set that in the AssemblyInfo.cs file. I don't think you could change it elsewhere. You couldn't set it dynamically during runtime anyway, so might as well set it there in not in a custom class.

You would need to recompile the assembly to change the name.

Justin
  • 6,373
  • 9
  • 46
  • 72
  • I don't think there is a restriction on where you put those directives, I just moved mine in to the main cs file and it worked fine. – Scott Chamberlain Jan 09 '13 at 20:44
  • Yeah, I don't need to set it dynamically, but I want to set it before I initialize an object that using this title... – inside Jan 09 '13 at 20:45
0

The property is attached to the compiled assembly and can't be modified at runtime. Unless you change it and write a new assembly file (as per the MSDN sample: http://msdn.microsoft.com/en-us/library/system.reflection.assemblytitleattribute.aspx).

If you need a dynamic application title, use something else.

RJ Lohan
  • 6,497
  • 3
  • 34
  • 54
0

The Visual Studio project templates generate the "AssemblyInfo.cs" by default, yet this is only a default location. In fact you can place the attribute in any file you would like. The only restriction is that you must place is in the "assembly scope", so the following code will not work:

namespace MySpace
{
    [assembly: System.Reflection.AssemblyVersion("1.0.0.0")]
}

The same applies to classes and all other blocks (does work neither).

class MyPlace
{
    [assembly: System.Reflection.AssemblyVersion("1.0.0.0")]
}

The assembly information specified in the source-code is embedded in the output assembly during the compilation process, so there is no way to "dynamically" change the values with code (as the code is executed only later, at runtime).

I personally really like to stick with the default version during all local work. The correct version of my assemblies is then set by some kind of build script which is triggered by the build server (Teamcity or Team Foundation Server).

In case you really want to change any properties of an assembly later on you might want to take a look at http://www.mono-project.com/Cecil

ollifant
  • 8,576
  • 10
  • 35
  • 45