是这样的,我的程序namespace WindowsApp
我在程序中要用到这个名字,所以用了下面的代码
CreateForm("WindowsApp." + this.TOOLS.SelectedNode.Name, "WindowsApp", this);
如果是这个问题,要改成什么取这个名字

解决方案 »

  1.   

    CreateForm这个函数的内部实现是什么。
      

  2.   

    主要是通过选择Treeview菜单生成子窗体        private void TOOLS_AfterSelect(object sender, TreeViewEventArgs e)
            {
                if (this.TOOLS.SelectedNode.Text != null)
                {
                    if (this.TOOLS.SelectedNode.Name == "Work")
                    {
                        CreateForm(Program.AppName + "." + Program.Usergroup, Program.AppName, this);
                    }
                    else
                    {
                        CreateForm(Program.AppName + "." + this.TOOLS.SelectedNode.Name, Program.AppName, this);
                    }
                }
                this.Text = "Parker Cylinder Tools    之    " + this.TOOLS.SelectedNode.Text;
            }
            public static void CreateForm(string strName, string namespaceName, Form MdiParentForm)
            {
                int Index = strName.LastIndexOf(".");
                string formName = strName.Substring(Index + 1);
                if (!ShowChildForm(formName, MdiParentForm))
                {
                    string path = namespaceName;
                    string name = strName;
                    Form fr = (Form)Assembly.Load(path).CreateInstance(name);
                    fr.MdiParent = MdiParentForm;
                    fr.Width = 1040;
                    fr.Height = 755;
                    fr.FormBorderStyle = FormBorderStyle.None;
                    fr.StartPosition = FormStartPosition.Manual;
                    fr.Show();
                }
            }
      

  3.   

    我现在全部定变量“WindowsApp”改成了下面的动态变量
    AppName = Assembly.GetExecutingAssembly().FullName;
    AppName = AppName.Substring(0, AppName.IndexOf(","));
    但是运行结果是一样的错误
      

  4.   

    orm fr = (Form)Assembly.Load(path).CreateInstance(name);
    path是什么
      

  5.   

    string path = namespaceName;
      

  6.   

    我调试时得到的值就是WindowsApp
      

  7.   

    namespaceName是什么。
    反正是写死了WindowsApp
    所以你修改了别的就出不来了。
      

  8.   

    那应该怎么写呀?不懂呀,刚学的时候调了好久才知道那个是要用代码最前面的namespace后面跟着的名字,所以开始就写成死的了,现在在Program里面定义了一个公共变量AppName
    AppName = Assembly.GetExecutingAssembly().FullName;
    AppName = AppName.Substring(0, AppName.IndexOf(","));
    但是生成的文件改名字后还是错误的
    所以想知道那个生成子窗体的真实正确的代码是怎么样?
      

  9.   

    当然不应该写死,应该用assembly.getexecutingassembly().codebase得到程序文件名,这样就不怕改了。
      

  10.   

    是改成这样吗AppName = Assembly.GetExecutingAssembly().CodeBase;
    但是这样会出错创建不了子窗体
      

  11.   

    你的意思那个变量应该是这个文件名而不是项目名?AppName = Path.GetFileNameWithoutExtension(Application.ExecutablePath);
    ************** 异常文本 **************
    System.IO.FileLoadException: 未能加载文件或程序集“Windows”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)
    文件名:“Windows”
       在 System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)
       在 System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)
       在 System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
       在 System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
       在 System.Reflection.Assembly.Load(String assemblyString)
       在 WindowsApp.MDIBOX.CreateForm(String strName, String namespaceName, Form MdiParentForm)
       在 WindowsApp.MDIBOX.TOOLS_AfterSelect(Object sender, TreeViewEventArgs e)
       在 System.Windows.Forms.TreeView.OnAfterSelect(TreeViewEventArgs e)
       在 System.Windows.Forms.TreeView.TvnSelected(NMTREEVIEW* nmtv)
       在 System.Windows.Forms.TreeView.WmNotify(Message& m)
       在 System.Windows.Forms.TreeView.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)我该怎么知道错误要怎么改
      

  12.   

    你需要去掉路径中的扩展名,搞得和你原来一样。自己琢磨下。我检查看出错原因可能在下面CreateForm(Program.AppName + "." + this.TOOLS.SelectedNode.Name, Program.AppName, this);
    最可能出错的是后面那个this,它的值是class WindwosApp.MDIBOX
    文件改名后就不是WindowsApp,我不知道要怎么改了
      

  13.   

    Program.AppName是什么,在Program.cs中定义的吧。
      

  14.   

    是的,那里面定义的一个公共变量,取得值是程序文件名,昨晚修改之前是下面这样的CreateForm("WindowsApp." + this.TOOLS.SelectedNode.Name, "WindowsApp", this);说道修改文件名后运行错误,所以定义了那个变量取文件名,结果还是错误……
      

  15.   

    Debug一下就可以了吧,运行exe,用Attach to process进行调试。
      

  16.   

    看了,代码修改起来不算太难:public static void CreateForm(string filePath,  string strName, string namespaceName, Form MdiParentForm)
            {
                int Index = strName.LastIndexOf(".");
                string formName = strName.Substring(Index + 1);
                if (!ShowChildForm(formName, MdiParentForm))
                {
                    string path = namespaceName;
                    string name = strName;
                    Assembly assembly = Assembly.LoadFrom(filePath);
                    Form fr = (Form)assembly.CreateInstance(name);
                    fr.MdiParent = MdiParentForm;
                    fr.Width = 1040;
                    fr.Height = 755;
                    fr.FormBorderStyle = FormBorderStyle.None;
                    fr.StartPosition = FormStartPosition.Manual;
                    fr.Show();
                }
            }调用的时候                if (this.TOOLS.SelectedNode.Name == "Work")
                    {
                        CreateForm(Application.ExecutablePath,"WindowsApp" + "." + Program.userGroup, "WindowsApp", this);
                    }
                    else
                    {
                        CreateForm(Application.ExecutablePath,"WindowsApp" + "." + this.TOOLS.SelectedNode.Name, "WindowsApp", this);
                    }
    原理是,你虽然改了exe文件的名字,但是assembly内部的自描述的manifest没有修改,manifest描述了assembly name等,Load是使用的assembly name,依旧是WindowsApp,修改成使用LoadFrom,从文件路径载入assembly。命名空间也依旧是WindowsApp,不随着你修改的文件名而改动。