Working with Path Strings


Working with Path Strings

Another way to refer to an assembly is to specify its location. Paths are normally relative to the location of the application. There's no big mystery to identifying an assembly by its path name , but there are a few tips to keep in mind.

To identify assemblies using path strings:

  • Use URLs in the form file:// or http://. For example:

     file://c:/chapter12/ordersXML.DLL or http://www.josemojica.com/games.DLL 
  • Include the name of the DLL plus the extension.

graphics/tick.gif Tips

  • Sometimes Web applications have problems locating files (DLLs and others) using relative paths. For example, the sample application creates a few XML files. Ideally we would like to save these files to the same directory where the application resides. The problem is that if you try to open a file using a relative path, your Web application is hosted by ASPNET_WP.EXE, and this EXE lives in the Windows/System32 directory. You can obtain the path to your application directory by using the code in Figure 12.8 .

    Figure 12.8 You use MapPath by requesting it to give you the full path of a file or of a directory. The file or directory doesn't need to exist. It's a way of asking, "if I had a file shared/myfile.txt, what would be its full path?"
     string filePath = System.Web.HttpContext.Current. Server.MapPath("filename.txt"); 
  • A tip for the tip above: MapPath only works if you ask it to map a file in the current directory or in a subdirectory below your application directory. It doesn't work if you request a path above the directory where your application is. For example requesting "..\..\something.txt" won't work properly. It'll return something in relation to where your Web server is installed rather than your Web application.

  • When you use the Load mechanism or LoadWithPartialName .NET looks for the DLL in the GAC first. If it doesn't find it there, it searches in the bin subdirectory of your application directory. It doesn't use the traditional PATH environment variable that DOS and some Windows programs rely on. It won't search the application directory either (just the bin subdirectory).




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