Loading a Program Dynamically with a Path String


Loading a Program Dynamically with a Path String

To load a program using its path:

  1. Type System.Reflection.Assembly ad , where ad is the name of the variable to hold a reference to the assembly you're going to load.

  2. Type an equal sign = .

  3. Type System.Reflection.Assembly.LoadFrom ( .

  4. Type the path to the DLL either in the traditional fashion ("c:\path\file.dll") or in URL form (with file:// or http://).

  5. Type a close parenthesis, followed by a semicolon ( Figure 12.11 ).

    Figure 12.11 LoadFrom uses the path to the assembly rather than the display string. You can use a URL to a local resource, to a network resource, or even to a Web site.
     //using System.Reflection; Assembly ad = Assembly.  LoadFrom  ( @"file://C:/csharpvqs/ordersXML.DLL"); 

graphics/tick.gif Tips

  • The LoadFrom mechanism isn't recommended for loading DLLs that are in the GAC. Instead, for DLLs in the GAC use the Load mechanism. However, if you need to download a DLL from a server in your network or from a Web site, LoadFrom is the preferred method.

  • You can specify a relative path with LoadFrom, but Web applications have problems with this approach, as I mentioned in an earlier tip. The problem is that they run from inside ASPNET_WP.EXE, which lives in the Windows\System32 directory. To find a file using a relative path, you first have to get the path to your application directory using the Server.MapPath function ( Figure 12.12 ).

    Figure 12.12 You can't really use relative paths with LoadFrom because they end up being mapped to Windows\System32 rather than your application directory. MapPath lets you get full paths based on your application directory.
     //using System.Reflection; string path = HttpContext.Current.  Server.MapPath  ("shared/ordersXML.DLL"); Assembly ad = System.Reflection.Assembly. LoadFrom(path); 



C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net