Microsoft KB Archive/818434

= PRB: 'System.IO.FileNotFoundException' When You Use the Assembly.LoadFrom Method to Load a DLL =

Article ID: 818434

Article Last Modified on 8/15/2005

-

APPLIES TO


 * Microsoft Visual J# .NET 2003 Standard Edition

-



SYMPTOMS
When you try to load a DLL in your Visual J# .NET application by using the Assembly.LoadFrom method, and the URL of the DLL contains encoded characters (such as %20 for the space character), you receive the following exception error message:

System.IO.FileNotFoundException: File or assembly name, or one of its dependencies, was not found.



CAUSE
The URL is made up of a restricted set of characters. The restricted set contains letters, numbers, and some symbols. This problem occurs because the Assembly.LoadForm method cannot interpret the encoded characters, such as %20, in the URL.



RESOLUTION
To resolve this problem, remove all percent sign (%) encoded characters from the URL before you pass it to the Assembly.LoadForm method. To remove the encoded characters programmatically, you may use the UrlUnescapeW Win32 API function. To load a DLL that has encoded characters in the URL by using Assembly.LoadFrom, follow these steps:  Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. In the New Project dialog box, click Visual J# Projects under Project Types, click Console Application under Templates, and then click OK. By default, ConsoleApplication1 is created.  Replace the existing code in Class1.jsl with the following sample code: import System.Reflection.*; import System.*; import System.Runtime.InteropServices.*;

public class Class1 {   // Importing the Win32 DLL shlwapi.dll /** @attribute DllImport(&quot;shlwapi.dll&quot;, CharSet=CharSet.Unicode ) */ static native long UrlUnescapeW(String url1, String url2, long i1[], long i2); // This method loads the DLL from the URL, and then tries to   // get the details of a specified method in the loaded DLL. public void GetMethodDetails(String url, String methodName) {       Assembly SampleAssembly; try {           SampleAssembly = Assembly.LoadFrom(Unescape(url)); MethodInfo Method = SampleAssembly.GetTypes[0].GetMethod(methodName); ParameterInfo[] Params = Method.GetParameters; for (int i=0; i<Params.length; i++) {               ParameterInfo Param = Params[i]; System.Console.WriteLine(&quot;Param=&quot; + Param.get_Name); System.Console.WriteLine(&quot; Type=&quot; + Param.get_ParameterType); System.Console.WriteLine(&quot; Position=&quot; + Param.get_Position); System.Console.WriteLine(&quot; Optional=&quot; + Param.get_IsOptional); }       }        catch(System.Exception excep) {           System.out.println(excep.ToString); }       finally {           System.Console.Read; }   }

// This method removes the encoded characters in the URL by using Win32 API, // and then returns the URL without the encoded characters. public String Unescape(String url) {       String outUrl = new String(' ', 260); long[] j = {260}; UrlUnescapeW(url, outUrl, j, 0); System.out.println(outUrl); return outUrl; }

// Main method. public static void main(String[] args) {       Class1 myObj = new Class1; String url = &quot;http://localhost/My%20JSharp%20Class%20Library/bin/debug/My%20JSharp%20Class%20Library.dll&quot;; myObj.GetMethodDetails(url, &quot;Cube&quot;); } }  On the Debug menu, click Start.



STATUS
This behavior is by design.

<div class="moreinformation_section">

Steps to Reproduce the Behavior
 Start Visual Studio .NET.</li> On the File menu, point to New, and then click Project.</li> In the New Project dialog box, click Visual J# Projects under Project Types, and then click Class Library under Templates.</li> In the Name text box, type My JSharp Class Library .</li> In the Location text box, type C:\Inetpub\wwwroot\ .</li> Click OK.</li>  Replace the existing code in Class1.jsl with the following code: public class Class1 {   public static long Squre(long num) {       return num*num; }   public static long Cube(long num) {       return num*num*num; } } </li> On the Build menu, click Build Solution.</li> Make sure that the DLL is available in the URL http://localhost/My JSharp Class Library/bin/debug/.</li> Start another instance of Visual Studio .NET.</li> On the File menu, point to New, and then click Project.</li> In the New Project dialog box, click Visual J# Projects under Project Types, click Console Application under Templates, and then click OK.</li>  Replace the existing code in Class1.jsl with the following sample code: import System.Reflection.*; import System.*;

public class Class1 {   // This method loads the DLL from the URL, and then tries to    // get the details of a specified method in the loaded DLL. public void GetMethodDetails(String url, String methodName) {       Assembly SampleAssembly; try {           SampleAssembly = Assembly.LoadFrom(url); MethodInfo Method = SampleAssembly.GetTypes[0].GetMethod(methodName); ParameterInfo[] Params = Method.GetParameters; for (int i=0; i<Params.length; i++) {               ParameterInfo Param = Params[i]; System.Console.WriteLine(&quot;Param=&quot; + Param.get_Name); System.Console.WriteLine(&quot; Type=&quot; + Param.get_ParameterType); System.Console.WriteLine(&quot; Position=&quot; + Param.get_Position); System.Console.WriteLine(&quot; Optional=&quot; + Param.get_IsOptional); }       }        catch(System.Exception excep) {           System.out.println(excep.ToString); }       finally {           System.Console.Read; }   }

public static void main(String[] args) {       Class1 myObj = new Class1; String url = &quot;http://localhost/My%20JSharp%20Class%20Library/bin/debug/My%20JSharp%20Class%20Library.dll&quot;; myObj.GetMethodDetails(url, &quot;Cube&quot;); } } </li> On the Debug menu, click Start.</li></ol>

<div class="references_section">