无法加载DLL-- FileNotFound?

时间:2015-01-18 23:40:48

标签: c#

image == http://i.stack.imgur.com/yUDS8.png

代码:

public void loadDLLs()
    {
        Directory.CreateDirectory(System.Environment.CurrentDirectory + "\\modules");
        string[] filePaths = Directory.GetFiles(@""+System.Environment.CurrentDirectory+"\\modules", "*.dll");
       foreach (string STR in filePaths)
       {
           String nameOfDll = Path.GetFileName(STR).Split('.')[0];
            Assembly MyDALL = Assembly.Load(STR);
            Type MyLoadClass = MyDALL.GetType(nameOfDll + "." + nameOfDll);
            Command obj = (Command)Activator.CreateInstance(MyLoadClass);
           commands.Add(obj);
          }
    }

这里的错误是Assembly.Load(); 我已经尝试加载它的完整路径和模块\\ Speach.dll 似乎没什么用。

1 个答案:

答案 0 :(得分:2)

使用错误的方法加载给定文件名的程序集。 传递给Assembly.Load的字符串为The long form of the assembly name. 类似的东西:

"SampleAssembly, Version=1.0.2004.0, Culture=neutral, 
 PublicKeyToken=8744b20f8da049e3"

相反,您需要使用Assembly.LoadFileAssembly.LoadFrom

public void loadDLLs()
{
    Directory.CreateDirectory(System.Environment.CurrentDirectory + "\\modules");
    string[] filePaths = Directory.GetFiles(@""+System.Environment.CurrentDirectory+"\\modules", "*.dll");
   foreach (string STR in filePaths)
   {
        String nameOfDll = Path.GetFileName(STR).Split('.')[0];
        Assembly MyDALL = Assembly.LoadFile(STR);
        Type MyLoadClass = MyDALL.GetType(nameOfDll + "." + nameOfDll);
        Command obj = (Command)Activator.CreateInstance(MyLoadClass);
        commands.Add(obj);
   }
}