. Net CF get the file name and full path of the current DLL and its calling program

Source: Internet
Author: User

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:

  1. Public Static Void Getname()
  2. {
  3. // The Name Of The current DLL-related collection
  4. Debug.Writeline(Path.Getfilename(Assembly.Getexecutingassembly().Getname().Codebase));
  5. Debug.Writeline(Assembly.Getexecutingassembly().Getmodules() [0].Name);
  6. Debug.Writeline(Assembly.Getexecutingassembly().Manifestmodule.Name);
  7. Debug.Writeline("");
  8. Debug.Writeline(Assembly.Getexecutingassembly().Getname().Codebase);
  9. Debug.Writeline(Assembly.Getexecutingassembly().Getmodules() [0].Fullyqualifiedname);
  10. Debug.Writeline(Assembly.Getexecutingassembly().Manifestmodule.Fullyqualifiedname);
  11. Debug.Writeline("");
  12.  
  13. // Name of the relevant Assembly of the caller
  14. Debug.Writeline(Path.Getfilename(Assembly.Getcallingassembly().Getname().Codebase));
  15. Debug.Writeline(Assembly.Getcallingassembly().Getmodules() [0].Name);
  16. Debug.Writeline(Assembly.Getcallingassembly().Manifestmodule.Name);
  17. Debug.Writeline("");
  18. Debug.Writeline(Assembly.Getcallingassembly().Getname().Codebase);
  19. Debug.Writeline(Assembly.Getcallingassembly().Getmodules() [0].Fullyqualifiedname);
  20. Debug.Writeline(Assembly.Getcallingassembly().Manifestmodule.Fullyqualifiedname);
  21. }

 

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:

    1. Testclass.Getname();

 

The observed results are as follows:

  1. Testdll. dll
  2. Testdll. dll
  3. Testdll. dll
  4.  
  5. \ Program Files \ getapplicationname \ testdll. dll
  6. \ Program Files \ getapplicationname \ testdll. dll
  7. \ Program Files \ getapplicationname \ testdll. dll
  8.  
  9. Getapplicationname.exe
  10. Getapplicationname.exe
  11. Getapplicationname.exe
  12.  
  13. \ Program Files \ getapplicationname \ getapplicationname.exe
  14. \ Program Files \ getapplicationname \ getapplicationname.exe
  15. \ 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:

    1. Debug.Writeline(Appdomain.Currentdomain.Friendlyname);

Result:

    1. 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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.