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.
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.
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.
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.
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.
Try:
System.IO.Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().Location)
Use the Path
class instead of trying to reinvent the wheel and calculating the substring indexes manually.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With