我用vs2008做了个CAB的工程,然后建了另外一个class lib的moudle,然后分别把这两个工程的build output路径设置到统一的文件夹下,然后build成功,点击那个.exe文件也可以运行。一切正常。但是当我用vs2008的run运行的时候,总是报错,说需要的那个dll找不到。就是那个主程序总是在它所在的文件夹下debug下寻找另外一个dll文件。是不是vs2008还需要设置什么属性才能运行这个工程啊?
不知道我说明白了没有。麻烦高手好心告诉一下,不胜感激。平时都是用vs2005的,刚用vs2008。很多地方不一样。找不到啊。

解决方案 »

  1.   

    默认就是在同一目录下查找Dll
    如果你非要把那些dll放到别的地方,那就不要用引用,用反射吧。
      

  2.   

    也许我没有说明白。
    我首先建立一个main shell程序,用它来加载其他moudle,加载的maoudle都在xml里配置。main shell通过读xml,然后加载相应的moudle的dll文件。
    我认为不用添加引用,因为我在output的文件夹下直接点击那个shell的.exe文件是能够直接运行起来的。只是用vs2008的start debug(就是run F5)的时候,那个main shell总是在自己所在目录下的bin文件夹下寻找需要加载的其他moudle的dll文件。所以我觉得是不是vs2008需要设置什么属性,或者设置什么的。是不是工作路径啊?如果是,怎么设置才对?我设置的工作路径和out path是一样的。但是设置完了后他还是去那个bin下找?不解。
    不知道大家看明白我的描述了没有。希望能给个解答。谢谢!
      

  3.   

    难道VS2005, VS2003, VS2002不是这样么?
      

  4.   

    如果需要引用其他工程的话,我就不需要在在xml里配置了,而且我想做的是用main shell动态加载一些dll文件。如果在main shell工程里引用其他的dll文件的话,就不能实现动态加载了。同样的工程,我在vs2005里不加引用就可以用start debug运行,但是在vs2007里就不行。他就会固执的在自己的bin文件里找寻需要的dll文件。
      

  5.   

    配置文件
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
       <configSections>
          <sectionGroup name="IocInCSharp">
             <section name="objects" type="IocInCSharp.ConfigHandler, MainApp" />
          </sectionGroup>
       </configSections>
       <IocInCSharp>
          <objects>
             <object name="SayHello" assembly="SayHello.dll" typeName="IocInCSharp.SayHello">
                <property name="HelloGenerator" assembly="HelloGenerator.dll" 
                          typeName="IocInCSharp.CnHelloGenerator"></property>
             </object>
          </objects>
       </IocInCSharp>
    </configuration>配置文件解析
    using System;
    using System.Configuration;
    using System.Xml;
    namespace IocInCSharp
    {
       public class ConfigHandler:IConfigurationSectionHandler
       {
          public object Create(object parent, object configContext, System.Xml.XmlNode section)
          {
             ObjectInfo info;
             PropertyInfo propInfo;
             ConfigInfo cfgInfo = new ConfigInfo();
             foreach(XmlNode node in section.ChildNodes)
             {
                info = new ObjectInfo();
                info.name = node.Attributes["name"].Value;
                info.assemblyName = node.Attributes["assembly"].Value;
                info.typeName = node.Attributes["typeName"].Value;
                foreach(XmlNode prop in node)
                {
                   propInfo = new PropertyInfo();
                   propInfo.propertyName = prop.Attributes["name"].Value;
                   propInfo.assemblyName = prop.Attributes["assembly"].Value;
                   propInfo.typeName = prop.Attributes["typeName"].Value;
                   info.properties.Add(propInfo);
                }
                cfgInfo.Objects.Add(info);
             }
             return cfgInfo;
          }
       }
    }
      

  6.   

    加载dll
    using System;
    using System.IO;
    using System.Configuration;
    using System.Reflection;
    namespace IocInCSharp
    {
       public class SayHelloFactory
       {
          public static object Create(string name)
          {
             Assembly assembly;
             object o = null;
             object p;
             string rootPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + 
                               Path.DirectorySeparatorChar;
             ConfigInfo cfgInfo = (ConfigInfo)ConfigurationSettings.GetConfig("IocInCSharp/objects"); 
             ObjectInfo info = cfgInfo.FindByName(name);
             if(info != null)
             {
                assembly = Assembly.LoadFile(rootPath + info.assemblyName);
                o = assembly.CreateInstance(info.typeName);
                Type t = o.GetType();
                for(int i=0; i<info.properties.Count; i++)
                {               
                   PropertyInfo prop = (PropertyInfo)info.properties[i];
                   
                   assembly = Assembly.LoadFile(rootPath + prop.assemblyName);
                   p = assembly.CreateInstance(prop.typeName);
                   t.InvokeMember(prop.propertyName, 
                      BindingFlags.DeclaredOnly | 
                      BindingFlags.Public | BindingFlags.NonPublic | 
                      BindingFlags.Instance | BindingFlags.SetProperty, null, o, new Object[] {p});
                }
             }
             return o;
          }
       }
    }