Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get executing assembly name using reflection

I am trying to pull the project name using the reflection, but during the substring method it give me "index out of bound error".

string s = System.Reflection.Assembly.GetExecutingAssembly().Location;           
int idx = s.LastIndexOf(@"\");
s = s.Substring(idx, s.Length);

I don't understand why it is giving error on the third line.

Plz Help.

like image 583
Karan Avatar asked Jan 31 '11 08:01

Karan


People also ask

Where can I find DLL assembly name?

If you know the assembly's file system path, you can call the static (C#) or Shared (Visual Basic) AssemblyName. GetAssemblyName method to get the fully qualified assembly name.

Which information can be extracted from an assembly using reflection?

NET Reflection allows application to collect information about itself and also manipulate on itself. It can be used effectively to find all the types in an assembly and/or dynamically invoke methods in an assembly. This includes information about the type, properties, methods and events of an object.

What is the method to load assembly by name?

Loads an assembly given its AssemblyName. The assembly is loaded into the domain of the caller using the supplied evidence. Loads the assembly with a common object file format (COFF)-based image containing an emitted assembly. The assembly is loaded into the application domain of the caller.

What is assembly GetExecutingAssembly ()?

In C#, GetExecutingAssembly() method is the method of Assembly class. This method returns the assembly that contains the code that is currently executing. To use this method we have to use System. Reflection in our program.


2 Answers

Try:

System.IO.Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().Location)
like image 126
Michel van Engelen Avatar answered Sep 21 '22 15:09

Michel van Engelen


Use the Path class instead of trying to reinvent the wheel and calculating the substring indexes manually.

like image 43
Ilya Kogan Avatar answered Sep 20 '22 15:09

Ilya Kogan