Windows Develop Bookmark and Share   
 index > Windows Forms General > How To Get AssemblyName of the application
 

How To Get AssemblyName of the application

I am trying to load a form dynamically at runtime.  I want to get a handle to the Form just from the form's name.  So far it seems like CreateInstanceWithUnwrap should do what I want.  Unfortunately I'm not sure how to get the AssemblyName of the Application itself.  Its a simple Windows Form app.  Here is the code I'm using:

AppDomain currentDomain = AppDomain.CurrentDomain;
object instance = currentDomain.CreateInstanceAndUnwrap( "", "Form2" );

I was assuming that if you didn't pass in the first parameter that the current Assembly would be assumed.  Unfortunately I get the error: String cannot have zero length.

Is there an easier way to do this?  Or how do I get the Assembly Name for the application itself.  I looked in AssemblyInfo.cs but didn't see anything promising there...

Any ideas?

Thanks,
David House
MigrationUser 1  Monday, February 03, 2003 10:33 PM
You need to use either Assembly.GetExecutingAssembly() or Assembly.GetEntryAssembly(), depending on what you need.

I did the following and it worked.

First add your using statements:

<b>using System.Reflection;</b>

Then in a Button's click event:

<b>Form1 frm = (Form1)AppDomain.CurrentDomain.CreateInstanceAndUnwrap                                                                     (Assembly.GetEntryAssembly().FullName, typeof(Form1).ToString());
frm.ShowDialog();</b>

Good luck
 - mike
MigrationUser 1  Tuesday, February 04, 2003 1:55 AM
Thank you very much!  That got me in the right direction.  Here is the code I ended up with:

Form f2 = (Form)AppDomain.CurrentDomain.CreateInstanceAndUnwrap( Assembly.GetExecutingAssembly().FullName,"WindowsApplication6.Form2" );
f2.Show();

This way I don't have to have prior knowledge of the Form before loading and showing it.  The Assembly.GetExecutingAssembly().FullName was the piece that I was missing.  And also it has to be "WindowsApplication6.Form2", not just "Form2".  I figured that out by looking at the result of typeof(Form1).ToString().

Thanks for all the help!
David
MigrationUser 1  Tuesday, February 04, 2003 8:44 AM
I think you can do this with less code:

Assembly a = Assembly.GetExecutingAssembly();
Type formType = a.GetType("WindowsApplication6.Form2");
Form f = (Form)Activator.CreateInstance(formType);
f.Show();

This is more efficient than using an object handle API like CreateInstanceAndUnwrap.  This API is designed to be used when you need to communicate across an app domain boundary.
MigrationUser 1  Thursday, February 06, 2003 5:03 PM
Or, if you know exactly which form you want to deal with simply use typeof (or GetType in Visual Basic)

Form2 frm = (Form2)Activator.CreateInstance(typeof(Form2));
MigrationUser 1  Thursday, February 06, 2003 6:49 PM
Thanks for all the tips.  All the information really helps.. :)
MigrationUser 1  Friday, February 07, 2003 2:42 PM

You can use google to search for other answers

Custom Search

More Threads

• What event fires when tabbing to a hidden TextBox in VB.Net
• Need help drilling into object
• Need help with Base Form usage
• launch external application from inside a vb form and keep original form running.
• creating xls spread sheet from gridview. problem with formating/sorting in xls
• VB6 blocking tab indexing in C#
• Keeping an MDI child window on top always
• switching forms
• How Can I bring the Menu list of Recently Opened file in Forms?
• How Can I Progressively Display a JPEG image?