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

本文关键字:cs 文件 程序 另一个 磁盘 文件夹 一个 编译 | 更新日期: 2023-09-27 18:36:20

我想编译一个存在于磁盘上其他文件夹中的.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);

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

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

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您根据路径给出,但请记住给出小路径。