C# Source Code: How to shell/open processes (inc. how to dynamically call the csharp compiler)
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
C# Source Code
How to shell/open processes (inc. how to dynamically call the csharp compiler)
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Monday, July 26, 2004
Hits:
1822
Category:
Streaming/Files/Directories
Article:
Below are two examples of how to open a process. The first example shows you how to create and compile csharp code on the fly, the second shows the csharp equivalent of the ShellExecute api... using System; using System.Diagnostics; using System.IO; namespace VBusers.Processes { class testProcess { ///
/// Contains examples of how to open processes ///
[STAThread] static void Main(string[] args) { // --Method 1 demonstrates how to open a process, supplying a command line-- // --and reading it's standard output-- // Create a csharp source file string nl = Environment.NewLine; // Create a demo csharp project TextFileSave(@"C:\sample.cs", new string[]{"using System;","namespace test","{","class test", "{", "static void Main(string[] args)","{",@"Console.WriteLine(""Hello Andrew"");", "Console.ReadLine();","}","}","}"},true); Process proc = new Process(); // Start the CSharp Compiler proc.StartInfo.FileName = @"c:\winnt\microsoft.net\framework\v1.1.4322\csc.exe"; // Add the system.dll and create a sample.exe from the sample.cs source code proc.StartInfo.Arguments = @"/r:System.dll /out:sample.exe sample.cs"; proc.StartInfo.WorkingDirectory = @"C:\"; // Don't use the file association to start the process proc.StartInfo.UseShellExecute = false; // Read standard out proc.StartInfo.RedirectStandardOutput = true; // Start the process proc.Start(); // Print the results Console.WriteLine(proc.StandardOutput.ReadToEnd()); // Wait for the process to finish proc.WaitForExit(); // --Method 2 demonstrates how to open a process using it's file assocation-- proc = new Process(); // Open a spreadsheet proc.StartInfo.FileName = @"C:\Book1.xls"; // Use the xls file association to workout how to open the process proc.StartInfo.UseShellExecute = true; // Don't redirect output proc.StartInfo.RedirectStandardOutput = false; // Start the process proc.Start(); // Wait for the process to finish proc.WaitForExit(); } ///
/// Creates a file containing the specified tex. ///
///
The file name to save the text to. ///
The contents of the text file. ///
If True will overwrite any existing files ///
Returns true if the text has been saved to the file
public static bool TextFileSave(string filename, string[] contents, bool overwrite) { try { if (File.Exists(filename) == true ) { if(overwrite == false) { // The file already exists and overwrite has been set to false return false; } else { File.Delete(filename); } } StreamWriter sr = File.CreateText(filename); foreach(string line in contents) { sr.WriteLine(line); } sr.Close(); sr = null; return true; } catch(Exception ex) { Trace.WriteLine("Failed to write to " + filename + ". Error: " + ex.ToString(),"GSL.FileLib.TextFileSave"); return false; } } } }
Terms and Conditions
Support this site
Download a trial version of the best FTP application on the internet