Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to know if there was a way I could get C# to run commands from a text file. For example: I have built a program and it opens text files and if there is C# code inside it it will run it. But I know that it couldn't run every command available or else I'd have to make reference to every system component which would be tedious and unecessary in my case. But is there a way of running code inside a program from a text file?
Posted

You are looking for CSharpCodeProvider. Check out this link

http://support.microsoft.com/kb/304655[^]

http://stackoverflow.com/questions/4063285/missing-assembly-references-in-dynamically-compiled-code[^]

You HAVE to add the references using CompilerParameters.ReferencedAssemblies.Add(...) and I don't think there is a simple way to work around this. If you want to add required assemblies alone, you may have to build a C# parser yourself.

Of course that's not impossible and you could do it using YACC/Bison (parsers) and Lex (lexical analyzers) and then link them to your application, but obviously it isn't easy! But they are definitely an interesting read!
 
Share this answer
 
First of all, I know it is not a text file but that might help you running C# codes from a file.

What you might want to do is create a .bat file. And then execute this file.

Creta a bat file to run C# Code[^]

For running the bat file here is the code;

System.Diagnostics.Process proc = new System.Diagnostics.Process(); // Declare New Process
                proc.StartInfo.FileName = fileName;
                proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                proc.StartInfo.CreateNoWindow = true;
                proc.Start();
                proc.WaitForExit();


Basically this will do it tho;

System.Diagnostics.Process.Start(@"C:\listfiles.bat");
 
Share this answer
 
v2
Comments
Pong D. Panda 14-Apr-11 22:22pm    
Well deserved 5!
Karthik. A 14-Apr-11 22:26pm    
I believe the poster is asking about compiling and running C# inside text files, and not just DOS commands inside a .bat file
Karthik. A 14-Apr-11 22:30pm    
Ok, I get your point. I would add this to your answer - You can invoke csc.exe like this System.Diagnostics.Process.Start(@"path_to_csc_compiler\csc.exe " + input_file); with the input .cs files passed, but the poster's problem would still exist.
Codemonkey3 14-Apr-11 22:51pm    
I have selected this as the anwser to my question. However the credit goes to Karthik for his contribution
It all depends on what you want to have for commands.

If this is C# code, it is quite solvable, too, but involves a lot of programming.
You can find all the details in my past Answers and references on this page:
code generating using CodeDom[^].

—SA
 
Share this answer
 
Try this article A tool for dynamic compile and run of C# or VB.NET code[^]There are similar topics on the side pane.
 
Share this answer
 
v2

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