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



The information in this article applies to:

  • Microsoft Visual J# .NET 2002

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 My%20JSharp%20Class%20Library.dll, 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:
  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. 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.
  4. 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("shlwapi.dll", 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("Param=" + Param.get_Name());
    				System.Console.WriteLine("  Type=" + Param.get_ParameterType());
    				System.Console.WriteLine("  Position=" + Param.get_Position());
    				System.Console.WriteLine("  Optional=" + 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 = "http://localhost/My%20JSharp%20Class%20Library/bin/debug/My%20JSharp%20Class%20Library.dll";
    		myObj.GetMethodDetails(url, "Cube");
    	}
    }
    
  5. On the Debug menu, click Start.

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Start Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. In the New Project dialog box, click Visual J# Projects under Project Types, and then click Class Library under Templates.
  4. In the Name text box, type My JSharp Class Library.
  5. In the Location text box, type C:\Inetpub\wwwroot\.
  6. Click OK.
  7. 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;
    	}
    }
  8. On the Build menu, click Build Solution.
  9. Make sure that the DLL is available in the URL http://localhost/My JSharp Class Library/bin/debug/.
  10. Start another instance of Visual Studio .NET.
  11. On the File menu, point to New, and then click Project.
  12. In the New Project dialog box, click Visual J# Projects under Project Types, click Console Application under Templates, and then click OK.
  13. 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("Param=" + Param.get_Name());
    				System.Console.WriteLine("  Type=" + Param.get_ParameterType());
    				System.Console.WriteLine("  Position=" + Param.get_Position());
    				System.Console.WriteLine("  Optional=" + 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 = "http://localhost/My%20JSharp%20Class%20Library/bin/debug/My%20JSharp%20Class%20Library.dll";
    		myObj.GetMethodDetails(url, "Cube");
    	}
    }
    
  14. On the Debug menu, click Start.

REFERENCES

For more information about how to name URLs, visit the following W3C Web site:

Modification Type:MinorLast Reviewed:8/15/2005
Keywords:kbDLL kbCompiler kberrmsg kbURLMon kbprb KB818434 kbAudDeveloper