In DLL, you sometimes need to use the main callProgramTo obtain the name and path of the calling program correctly. This must be distinguished from the name and path of the DLL itself!
This involves the use of the system. reflection. Assembly class.
Getexecutingassembly: Gets the Assembly containing the Code currently executed
Getcallingassembly: Returns the system. reflection. Assembly that calls the method currently being executed.
The following are static functions in a DLL. Note the differences:
-
- Public Static Void Getname()
-
- {
-
- // The Name Of The current DLL-related collection
- Debug.Writeline(Path.Getfilename(Assembly.Getexecutingassembly().Getname().Codebase));
-
- Debug.Writeline(Assembly.Getexecutingassembly().Getmodules() [0].Name);
- Debug.Writeline(Assembly.Getexecutingassembly().Manifestmodule.Name);
-
- Debug.Writeline("");
-
- Debug.Writeline(Assembly.Getexecutingassembly().Getname().Codebase);
- Debug.Writeline(Assembly.Getexecutingassembly().Getmodules() [0].Fullyqualifiedname);
-
- Debug.Writeline(Assembly.Getexecutingassembly().Manifestmodule.Fullyqualifiedname);
-
- Debug.Writeline("");
-
-
- // Name of the relevant Assembly of the caller
-
- Debug.Writeline(Path.Getfilename(Assembly.Getcallingassembly().Getname().Codebase));
-
- Debug.Writeline(Assembly.Getcallingassembly().Getmodules() [0].Name);
- Debug.Writeline(Assembly.Getcallingassembly().Manifestmodule.Name);
-
- Debug.Writeline("");
-
- Debug.Writeline(Assembly.Getcallingassembly().Getname().Codebase);
- Debug.Writeline(Assembly.Getcallingassembly().Getmodules() [0].Fullyqualifiedname);
-
- Debug.Writeline(Assembly.Getcallingassembly().Manifestmodule.Fullyqualifiedname);
-
- }
The Dynamic Static environment is in the testclassclass, and then the static environment is called in the calling program, and the calling program is getapplicationname.exe ", as follows:
- Testclass.Getname();
The observed results are as follows:
-
- Testdll. dll
-
- Testdll. dll
-
- Testdll. dll
-
-
-
- \ Program Files \ getapplicationname \ testdll. dll
-
- \ Program Files \ getapplicationname \ testdll. dll
-
- \ Program Files \ getapplicationname \ testdll. dll
-
-
- Getapplicationname.exe
-
- Getapplicationname.exe
-
- Getapplicationname.exe
-
-
-
- \ Program Files \ getapplicationname \ getapplicationname.exe
-
- \ Program Files \ getapplicationname \ getapplicationname.exe
-
- \ Program Files \ getapplicationname \ getapplicationname.exe
There is another way, but you can only get the name of the calling program, not the full path, that is throughAppdomain. currentdomain. friendlynameObtain, as follows:
- Debug.Writeline(Appdomain.Currentdomain.Friendlyname);
Result:
- Getapplicationname.exe
You can also refer to this articleArticle: Write and design friendly controls for Windows Mobile.Appdomain. currentdomain. friendlynameI have introduced it.
CodeDownload:
Getapplicationname.rar