error occurred when programmatically compile
Hi,
An error occurred when programmatically compile another project from my present project.
The source code
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;
using System.Diagnostics;
using System.IO;
using System.Globalization;
namespace Compile2
{
class Class2
{
/// <summary>
/// The main entry point for the application.
/// </summary>
public bool CompileExecutable(String sourceName)
{
FileInfo sourceFile = new FileInfo(sourceName);
//CodeDomProvider provider = null;
CSharpCodeProvider provider = new CSharpCodeProvider();
ICodeCompiler compiler = provider.CreateCompiler();
bool compileOk = false;
// provider = new Microsoft.CSharp.CSharpCodeProvider();
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(".", "_"));
string[] Myref ={ "System.dll","System.Drawing.dll","System.Windows. Forms.dll","System.Data.dll","System.Drawing.dll" };
string MyAsm = "MyAsm.dll";
CompilerParameters cp = new CompilerParameters(Myref, MyAsm);
// 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 = compiler.CompileAssemblyFromFile(cp, sourceName);
//CompilerResults cr = new CompilerResults();
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;
}
}
class Program
{
static void Main(string[] args)
{
Class2 c2 = new Class2();
c2.CompileExecutable("C:\\temp3\\temp3\\Program.cs ");
Console.WriteLine("hai");
Console.Read();
}
}
}
expecting quick response
thanks
ramesh
|