Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm trying to run a cs file(with arguments) that is in my explorer(not through my IDE). There will be a single method named "main" in the cs file , I want to run that with arguments through my console app.

EDIT:

If your here for the same thing, :-]

compiler construction - Is it possible to dynamically compile and execute C# code fragments? - Stack Overflow[^]
Using the CodeDOM | Microsoft Docs[^]
Generating and Compiling Source Code from a CodeDOM Graph | Microsoft Docs[^] // contained what I need

What I have tried:

did searching can't find a good answer, a answer that is working for me. I usually run into a topic involving 'running a cs file through my IDE', I'm trying to a run cs file that is in my explorer through my console app.
Posted
Updated 20-Jul-18 18:50pm
v3

To do that, you need to compile it - that's OK because every .NET installation includes the C# compiler, CSC.EXE: Compiling C# Code at Runtime[^]
You can then run it using Process.Start (if it compiles cleanly): Process.Start Method (System.Diagnostics)[^]
 
Share this answer
 
Comments
Thaana Paana 20-Jul-18 12:02pm    
c# compiling seems to be in the quite expertise area, but I'm gonna give it a try and do you know any other API capable of compiling c# at runtime? also should have searched 'vs c# runtime compile and execute' on google + here is a similar topic if anybody is interested.

https://stackoverflow.com/questions/826398/is-it-possible-to-dynamically-compile-and-execute-c-sharp-code-fragments
OriginalGriff 20-Jul-18 12:22pm    
There's Mono - but that's even more "expert required" as Mono isn't installed with .NET so your deployment program would have to handle that - and it's a BIG package!

Compiling isn't difficult: the link provides an example.
You can't execute a cs file, a cs file is just a text file. The IDE converts that text into compiled code that can be run.
 
Share this answer
 
You cannot invoke it directly.

What you can do is select, compile, and then execute on-the-fly within the calling program
 
Share this answer
 
Comments
Thaana Paana 20-Jul-18 12:06pm    
thanks for the useful info, didn't know it worked that way! have been coding for a while now(self taught c#) xd

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900