tags:

views:

36

answers:

3

Can we execute a batch file (*.bat) using Application Domain?

Thank you in advance.

+3  A: 

No. AppDomain is a purely CLRs' concept, whereas *.bat files are executed "inside" usual Windows.

Anton Gogolev
Apart from my question above, Application Domain cannot execute unmanaged assemblies?Frankly, I am new to Application Domain.
xport
@xport Application Domain can execute unmanaged C++/CLI assemblies, but a batch file does not constitute an unmanaged assembly.
Richard Szalay
+1, but I would add batch files run in there own process as any other executable.
kenny
+2  A: 

If you need to execute a batch file from your C#, use the Process class.

Process myProcess = new Process();
myProcess.StartInfo.FileName = "C:\\HelloWorld.bat";
myProcess.Start();

Batch files are not part of the CLR and therefor cannot be run within an AppDomain.

Oded
Your code snippet calls the batch file using process rather than ApplicationDomain. I intentionally choose ApplicationDomain because of its efficiency aspect.
xport
My scenario does not allow multiple processes to run; it will break the scalability.
xport
@xport - fair enough. You did not provide that detail in your question, and I assumed you may have not known about `Process`. In this case, I would have to agree with @Anton Gogolev - it is not possible to run a batch file within an AppDomain.
Oded
@Oded, thank you for your response.
xport
+2  A: 

Application Domains are a purely CLR concept, they have no relevancy to anything that's not a managed library, thus there's no way to run a batch file within an appdomain. The Process object that the batch file runs under will be tied to one specific appdomain, but it will still result in a separate process being instantiated to run your batch file.

There's some fairly good explanatory text under Remarks in the MSDN AppDomain Class documentation that should help you get a handle on what precisely an appdomain is, and what you can and can't do with one.

Rob