是这样的,我的程序namespace WindowsApp
我在程序中要用到这个名字,所以用了下面的代码
CreateForm("WindowsApp." + this.TOOLS.SelectedNode.Name, "WindowsApp", this);
如果是这个问题,要改成什么取这个名字
我在程序中要用到这个名字,所以用了下面的代码
CreateForm("WindowsApp." + this.TOOLS.SelectedNode.Name, "WindowsApp", this);
如果是这个问题,要改成什么取这个名字
{
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();
}
}
AppName = Assembly.GetExecutingAssembly().FullName;
AppName = AppName.Substring(0, AppName.IndexOf(","));
但是运行结果是一样的错误
path是什么
反正是写死了WindowsApp
所以你修改了别的就出不来了。
AppName = Assembly.GetExecutingAssembly().FullName;
AppName = AppName.Substring(0, AppName.IndexOf(","));
但是生成的文件改名字后还是错误的
所以想知道那个生成子窗体的真实正确的代码是怎么样?
但是这样会出错创建不了子窗体
************** 异常文本 **************
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)我该怎么知道错误要怎么改
最可能出错的是后面那个this,它的值是class WindwosApp.MDIBOX
文件改名后就不是WindowsApp,我不知道要怎么改了
{
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,不随着你修改的文件名而改动。