0

I'm trying to get the project name via code (TestGetAssemblyName):

enter image description here

I've tried System.Reflection.Assembly.GetExecutingAssembly().GetName().Name but this returns some weird jumble of characters, looks dynamically generated. Is there a way to get TestGetAssemblyName returned to me via code?

DocMax
  • 12,094
  • 7
  • 44
  • 44
Mike Marks
  • 10,017
  • 17
  • 69
  • 128

2 Answers2

1

What you read in Solution Explorer is completely unrelated to compiled assembly name or assembly title. Moreover it won't be written anywhere in your compiled assembly so you can't access that name from code.

Assuming MyType is a type defined in your assembly you can use AssemblyTitleAttribute attribute (usually defined in AssemblyInfo.cs) to read given title (unrelated to compiled assembly name or code namespace):

var assembly = typeof(MyType).Assembly;
var attribute = assembly
    .GetCustomAttributes(typeof(AssemblyTitleAttribute), true)
    .OfType<AssemblyTitleAttribute>()
    .FirstOrDefault();

var title = attribute != null ? attribute.Title : "";

This (AssemblyTitleAttribute) is just one of the attributes defined in AssemblyInfo.cs, pick the one that best fit your requirement.

On the other way you may use assembly name (Name property from Assembly) to get compiled assembly name (again this may be different from assembly title and from project title too).

EDIT
To make it single line you have to put it (of course!) somewhere in your code:

public static class App
{
    public static string Name
    {
         get
         {
             if (_name == null)
             {
                 var assembly = typeof(App).Assembly;
                 var attribute = assembly
                     .GetCustomAttributes(typeof(AssemblyTitleAttribute), true)
                     .OfType<AssemblyTitleAttribute>()
                     .FirstOrDefault();

                _name = attribute != null ? attribute.Title : "";
             }

             return _name;
         }
    }

    private string _name;
}

To be used as:

<a href="server/App?appName=<% App.Name %> /> 
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
0

I see your project is a web site.

The problem is that web sites generate a dll for every single folder you have in them. That's the various App_Web_asdkfjh.dll.

If you publish the web site, VS will combine all those DLLs into one. You can name this however you want, even including the name of your project, in properties of the deployment project.

Without having a deployment project, you're stuck with the random looking file names.

Luaan
  • 62,244
  • 7
  • 97
  • 116