编译.cs文件,该文件位于另一个.cs程序的磁盘的不同文件夹中

时间:2012-06-06 08:55:33

标签: c#

我想编译一个存在于磁盘上其他文件夹中的.cs程序,并使用另一个.cs程序或控制台应用程序或窗口应用程序生成它的.dll。我尝试使用以下

using (StreamReader textReader = new StreamReader(@"path\Filename.cs"))
{
     textFile = textReader.ReadToEnd();
    Console.WriteLine(textFile);
}
CodeDomProvider codeProvider = new CSharpCodeProvider(); 
ICodeCompiler compiler = codeProvider.CreateCompiler(); 
// add compiler parameters 
CompilerParameters compilerParams = new CompilerParameters();
compilerParams.CompilerOptions = "/target:library /optimize"; 
compilerParams.GenerateExecutable = false; 
compilerParams.GenerateInMemory = true;             
compilerParams.IncludeDebugInformation = false; 
compilerParams.ReferencedAssemblies.Add("mscorlib.dll");
compilerParams.ReferencedAssemblies.Add("System.dll");  
// compile the code 
CompilerResults results = compiler.CompileAssemblyFromSource(compilerParams,        textFile);

1 个答案:

答案 0 :(得分:0)

我有这样的想法,直到你找不到具体的东西试试这个

Process processCS= new Process();
processCS.StartInfo.WorkingDirectory = @"C:\<your code directory>";
processCS.StartInfo.FileName = @"C:\Windows\Microsoft .NET\Framework\version\csc.exe   /out:yourdllname.dll yourcodefilename.cs";
processCS.StartInfo.CreateNoWindow = true;
processCS.Start();
processCS.WaitForExit();

你可以在cs文件中调用它来编译其他cs文件。检查它是否有效.... \

默认情况下,进程将搜索system32目录中的文件,因此请使用以下代码执行此操作...

ProcessStartInfo info = new ProcessStartInfo(@"C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe");
info.Arguments = @" /out:C:\ss\Class1.dll C:\ss\Class1.cs";
info.UseShellExecute = false;
Process.Start(info);

在论证中,我已经根据你的路径给出了cs文件的路径为C:\ ss \ Class1.cs,但请记住给出小路径。

相关问题