user3552431
user3552431

Reputation: 11

How can i run an .exe file with Assembly class

I want to run my exe file with Assembly class. Here is my codes

Assembly as = Assembly.LoadFile("path");
as.EntryPoint.Invoke(null,null);

Error:

Parameter count mismatch.

Upvotes: 0

Views: 3309

Answers (2)

Mike Zboray
Mike Zboray

Reputation: 40818

If you want to load an executable .NET assembly into your own process use AppDomain.ExecuteAssembly:

int exitCode = AppDomain.Current.ExecuteAssembly("path");

There is also ExecuteAssemblyByName if you have the assembly name instead of the path.

Upvotes: 0

Alexei Levenkov
Alexei Levenkov

Reputation: 100527

If it is native exe - use Process.Start, if it is managed (i.e. create with C#) you need to load assembly and than call Main via reflection.

Native exe:

  Process.Start("IExplore.exe");

It looks like you have managed assembly and you've already know Main entrypoint (via Assembly.EntryPoint property). You need to makes sure it is not null (unlikely) and pass correct arguments.

Main signatures are static void Main(string[] args) or static int Main(string[] args) or static void Main() so you need to pass null for instance in Invoke and correct number of arguments. If Main takes no parameters - use new object[0] for second parameter, otherwise construct string array with parameters and than wrap it in new object[]{args}.

Sample for case where Main takes arguments but does not do anything with them:

Assembly as = Assembly.LoadFile(@"c:\temp\my.exe");   
as.EntryPoint.Invoke(null, new object[]{new string[0]});     

Upvotes: 3

Related Questions