CSharpCodeProvider----------------------------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;
}From MSDN
{
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;
}From MSDN
解决方案 »
- <?xml namespace="" prefix="asp" ?> vs2005创建控件
- 初学求助:C# Invalid token ';'(VS2008下)
- java转net 怎么办?
- 有没有谁知道监控软件的界面是怎么做的啊?,最好有代码和视图,谢谢!
- 问一 datagridview 的属性
- c#连数据库的方法?????
- 有關編寫組件問題(急)
- 百分求base 64解碼/編碼程序代碼
- 我想给英文版的vs2003安装中文版的msdn,有没有高人会啊?
- 编写.NET控件,进军海外市场,请大家谈谈想法!
- HTTP协议转换问题??请教高手
- 新手在实训遇到一个问题关于博客相册的问题,希望感兴趣的朋友进来帮下忙
可以参考一下..............................