Created
November 9, 2013 12:17
-
-
Save yemrekeskin/7384863 to your computer and use it in GitHub Desktop.
to compiler code files when you generated dynamically codes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| if (args.Length > 0) | |
| { | |
| // First parameter is the source file name. | |
| if (File.Exists(args[0])) | |
| { | |
| CompileExecutable(args[0]); | |
| } | |
| else | |
| { | |
| Console.WriteLine("Input source file not found - {0}", | |
| args[0]); | |
| } | |
| } | |
| else | |
| { | |
| Console.WriteLine("Input source file not specified on command line!"); | |
| } | |
| Console.ReadLine(); | |
| } | |
| public static bool CompileExecutable(String sourceName) | |
| { | |
| FileInfo sourceFile = new FileInfo(sourceName); | |
| CodeDomProvider provider = null; | |
| bool compileOk = false; | |
| // Select the code provider based on the input file extension. | |
| if (sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) == ".CS") | |
| { | |
| provider = CodeDomProvider.CreateProvider("CSharp"); | |
| } | |
| else if (sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) == ".VB") | |
| { | |
| provider = CodeDomProvider.CreateProvider("VisualBasic"); | |
| } | |
| else | |
| { | |
| Console.WriteLine("Source file must have a .cs or .vb extension"); | |
| } | |
| if (provider != null) | |
| { | |
| // Format the executable file name. | |
| // Build the output assembly path using the current directory | |
| // and <source>_cs.exe or <source>_vb.exe. | |
| String exeName = String.Format(@"{0}\{1}.exe", | |
| System.Environment.CurrentDirectory, | |
| sourceFile.Name.Replace(".", "_")); | |
| CompilerParameters cp = new CompilerParameters(); | |
| // Generate an executable instead of | |
| // a class library. | |
| cp.GenerateExecutable = true; | |
| // Specify the assembly file name to generate. | |
| cp.OutputAssembly = exeName; | |
| // Save the assembly as a physical file. | |
| cp.GenerateInMemory = false; | |
| // Set whether to treat all warnings as errors. | |
| cp.TreatWarningsAsErrors = false; | |
| // Invoke compilation of the source file. | |
| CompilerResults cr = provider.CompileAssemblyFromFile(cp, | |
| sourceName); | |
| if (cr.Errors.Count > 0) | |
| { | |
| // Display compilation errors. | |
| Console.WriteLine("Errors building {0} into {1}", | |
| sourceName, cr.PathToAssembly); | |
| foreach (CompilerError ce in cr.Errors) | |
| { | |
| Console.WriteLine(" {0}", ce.ToString()); | |
| Console.WriteLine(); | |
| } | |
| } | |
| else | |
| { | |
| // Display a successful compilation message. | |
| Console.WriteLine("Source {0} built into {1} successfully.", | |
| sourceName, cr.PathToAssembly); | |
| } | |
| // Return the results of the compilation. | |
| if (cr.Errors.Count > 0) | |
| { | |
| compileOk = false; | |
| } | |
| else | |
| { | |
| compileOk = true; | |
| } | |
| } | |
| return compileOk; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment