编写控件的时候是生成.dll的文件,用Application.StartupPath;获取路径为d:\Program Files\Microsoft Visual Studio 8\Common7\IDE而不是平常项目的目录。用Directory.GetCurrentDirectory();获取当前的工作的目录,结果却是d:\data是我操作数据的目录。怎样才能获取的到生成.dll文件的目录路径呢?!

解决方案 »

  1.   

    Application.StartupPath 获取应用程序执行文件路径。不包括文件名。
    这个就是你编译好的dll文件路径
      

  2.   

    Koala_sea(阳光总在风雨后) 兄: 我本来也以为是:Application.StartupPath 这个就是你编译好的dll文件路径。但获取出来的路径就是我上面说的
      

  3.   

    我也遇到过这个问题,我是把一个连接字符串写再了xml文件里,然后再一个dll中读这个连接字符串,然后我再程序里引用这个dll,然后做了一个用户控件,当把这个用户控件拖到其他用户控件或者form上时就出错了,说是找不到xml文件.
    解决办法,因为这个dll是给自己用,Application.StartupPath,Directory.GetCurrentDirectory()而这两个获取路径的方法是在设计时,获得的路径不是我们要的,在我们运行我们的程序时是对的.
    所以我就做了一个判断,如果在运行时就从xml文件里取连接字符串.如果不是,就读程序里写死的那个
    msn:[email protected]
      

  4.   

    Assembly myAssembly = Assembly.GetEntryAssembly();
    string path = myAssembly.Location;
      

  5.   

    我这样做的!!
            /**//// <summary>
            /// 获取Assembly的运行路径
            /// </summary>
            /// <returns></returns>
            private string GetAssemblyPath()
            {
                string _CodeBase = System.Reflection.Assembly.GetExecutingAssembly().CodeBase ;            _CodeBase = _CodeBase.Substring(8,_CodeBase.Length - 8);    // 8是 file:// 的长度            string[] arrSection = _CodeBase.Split(new char[]{'/'});
                
                string _FolderPath = "";
                for(int i=0;i<arrSection.Length-1;i++) 
                {
                    _FolderPath += arrSection[i] + "/";
                }            return _FolderPath;
            }