0
votes

I want to create an assembly at run time like this:

AssemblyName an = new AssemblyName("ImanAssembly");
AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.Save);
ModuleBuilder mb = ab.DefineDynamicModule(an.Name, an.Name + ".exe");
TypeBuilder tb = mb.DefineType("Product", TypeAttributes.Public);
FieldBuilder fb = tb.DefineField("ProName", typeof(string), FieldAttributes.Public);
Type t = tb.CreateType();
ab.Save(an.Name + ".exe");

But I get this error in last line of my code:

An exception of type 'System.IO.IOException' occurred in mscorlib.dll but was not handled in user code

Additional information: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

Update: i solve that by run VS as admin...but why i cant find my created assembly in bin folder???? where can i find it ?

1
Run your VS as admin.Power Star
thanks @PowerStar it works...but i dont see my assembly in bin folder ... do you know where can i find it ?Iman Salehi
I have answered for this question. Check it may be help you.Power Star

1 Answers

2
votes

To get out from access denied problem you have to run your VS as Admin.

Below line will decide the directory path to store the assemble.

AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.Save);

In this you can specify the directory as third parameter. If you are not specified then, it will assume current directory.

So now you can find your exe file in C:\Program Files (x86)\IIS Express

If you want to save it in you bin folder then use below line.

    AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.Save, Server.MapPath("~/bin"));