在文件夹A下有run.exe、
B文件夹下有一个hello.dll,
run.exe运行怎么去调用hello.dll。
A,B文件夹同级一般编译后都会把引用的Dll放到统一目录下,那样太乱了,想整理一下习惯100分

解决方案 »

  1.   

    Assembly.LoadFromAssembly SampleAssembly;
    SampleAssembly = Assembly.LoadFrom("c:\\Sample.Assembly.dll");
    // Obtain a reference to a method known to exist in assembly.
    MethodInfo Method = SampleAssembly.GetTypes()[0].GetMethod("Method1");
    // Obtain a reference to the parameters collection of the MethodInfo instance.
    ParameterInfo[] Params = Method.GetParameters();
    // Display information about method parameters.
    // Param = sParam1
    //   Type = System.String
    //   Position = 0
    //   Optional=False
    foreach (ParameterInfo Param in Params)
    {
        Console.WriteLine("Param=" + Param.Name.ToString());
        Console.WriteLine("  Type=" + Param.ParameterType.ToString());
        Console.WriteLine("  Position=" + Param.Position.ToString());
        Console.WriteLine("  Optional=" + Param.IsOptional.ToString());
    }
      

  2.   

    好像有个Assembly类中有这样的方法。
      

  3.   

    你也可以把dll 放在GAC里面,这是全局共享的。
      

  4.   

    既然要调用,那么它们就是相关的项目,你应该先在run.exe中引用,要调用的hello.dll这样,VS在你运行run.exe时会自动生成hello.dll 它们都会位于一个默认位置(../bin/Debug/)下如果是以进程的方式通讯,那么你在启动run.exe以后,在联系,包含hello.dll的某一个进程就可以了,可以通过remoting来做这样的分布式设计,也可以用,MSMQ(消息机制)来做
      

  5.   

    用相对路径不行么?..\b\xxx.dll
    这样写行不行呢?
      

  6.   

    有三种方法解决这个问题
    Method 1: Install the assembly in the global assembly cache (GAC)
    Method 2: Use an application configuration (.config) file with the <codeBase> tags
    <configuration>
       <runtime>
          <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
             <dependentAssembly>
                <assemblyIdentity name="MyAssembly2"  culture="neutral" publicKeyToken="307041694a995978"/>
                <codeBase version="1.0.1524.23149" href="FILE://C:/Myassemblies/MyAssembly2.dll"/>
             </dependentAssembly>
          </assemblyBinding>
       </runtime>
    </configuration>Method 3: Use the AssemblyResolve event,net runtime在捆定assembly的时候,如果在本目录找不到,则调用这个函数,给开发者机会从其它地方载入
    private Assembly MyResolveEventHandler(object sender,ResolveEventArgs args)
    {
    //This handler is called only when the common language runtime tries to bind to the assembly and fails. //Retrieve the list of referenced assemblies in an array of AssemblyName.
    Assembly MyAssembly,objExecutingAssemblies;
    string strTempAssmbPath=""; objExecutingAssemblies=Assembly.GetExecutingAssembly();
    AssemblyName [] arrReferencedAssmbNames=objExecutingAssemblies.GetReferencedAssemblies();

    //Loop through the array of referenced assembly names.
    foreach(AssemblyName strAssmbName in arrReferencedAssmbNames)
    {
    //Check for the assembly names that have raised the "AssemblyResolve" event.
    if(strAssmbName.FullName.Substring(0, strAssmbName.FullName.IndexOf(","))==args.Name.Substring(0, args.Name.IndexOf(",")))
    {
    //Build the path of the assembly from where it has to be loaded.
    strTempAssmbPath="C:\\Myassemblies\\"+args.Name.Substring(0,args.Name.IndexOf(","))+".dll";
    break;
    } }
    //Load the assembly from the specified path. 
    MyAssembly = Assembly.LoadFrom(strTempAssmbPath); //Return the loaded assembly.
    return MyAssembly;
    }
    详细步骤见:
    How to load an assembly at runtime that is located in a folder that is not the bin folder of the application
    http://support.microsoft.com/kb/837908
      

  7.   

    一般编译后都会把引用的Dll放到统一目录下,那样太乱了.
    把用的东西放一起反倒乱了?
      

  8.   

    我不知道C#支持这种相对路径不。
    我再说一次我8L的方法。
    如果你的某一目录下有A,B两个文件夹,你目前在A文件夹下,那么你在地址栏键入“..\B”就可以进入同一级目录下的B目录了。
    ..\表示返回上一级目录
      

  9.   

    8楼的没有理解意思,是这样的一个解决方案里有两个项目,A, B,
    A项目是主程序,B项目是程序集。
    A项目引用了B项目,编译生成后A.exe和 b.dll默认就同在Debug目录。
    由于我有多个exe,多个dll,所以我想把exe放到自己建的一个bin目录。dll都放到自己建的lib目录。并保证exe运行时能访问到lib目录中的dll
      

  10.   

    是这样的一个解决方案里有两个项目,A, B, 
    A项目是主程序,B项目是程序集。 
    A项目引用了B项目,编译生成后A.exe和 b.dll默认就同在Debug目录。 
    由于我有多个exe,多个dll,所以我想把exe放到自己建的一个bin目录。dll都放到自己建的lib目录。并保证exe运行时能访问到lib目录中的dll 
      

  11.   

    我就是那个意思啊,比如EXE都在?:\XXX\XX\BIN
    DLL都在?:\XXX\XX\LIB(PS:这些问号,XX都替换成同样的值)
    如果EXE想调用DLL,那么用相对路径访问
    "..\lib\xx.dll"
    这样就从bin目录下进入了lib目录下访问到了需要的dll
      

  12.   

    刚才试了一些,很幸运,C#支持这种相对路径,我建立了一个“临时项目.sln”
    添加了一个checkedListBox,捕获到路径文件之后显示到checkedListBox以下是部分代码
    private void button1_Click(object sender, EventArgs e)
            {
                DirectoryInfo di = new DirectoryInfo(@"..\..\..\");
                FileInfo[] fi = di.GetFiles("*.*");
                checkedListBox1.Items.AddRange(fi);
            }结果checkedListBox显示出来两个项“临时项目.sln”和“临时项目.suo”与实际相符。
    (..\..\..\表示向上返回三层目录,即运行的时候是在VS项目文件夹下的“临时项目\临时项目\bin\debug”
    那么执行“..\..\..\”之后,向上返回三层目录,即VS项目文件夹下的“临时项目\”,捕获到了该目录下的两个文件,“临时项目.sln”和“临时项目.suo”)
      

  13.   

    再详细说一下,比如你的启动程序是a.exe,存放目录为D;\temp\bin\a.exe,需要调用上一层目录下lib\a.dll。
    那么你的程序编写的时候用相对路径的DLL就行了。
    挂载DLL的这一部分代码的DLL路径这么写“..\lib\a.dll”就行了,这样无论用户把程序安装到哪个目录都无所谓了,这个就是相对路径的好处
      

  14.   

    我不是要在EXE运行时加载程序集(挂载DLL),我是要找到A工程(exe)引用B工程的方式,
    你这是运行时加载程序集,这样写那我每个工程都得拆开,全都在主程序运行时加载,否则我编译都通不过。vs2005 中是一个解决方案里面有多个工程,每个工程可以相互引用,默认引用,编译后他们都处于同一级目录。