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

C# 编译一个.cs文件,该文件与另一个.cs程序位于磁盘的不同文件夹中,c#,C#,我想编译一个存在于磁盘上其他文件夹中的.cs程序,并使用另一个.cs程序、控制台应用程序或窗口应用程序生成该程序的.dll。我试着使用下面的 using (StreamReader textReader = new StreamReader(@"path\Filename.cs")) { textFile = textReader.ReadToEnd(); Console.WriteLine(textFile); } CodeDomProvider codeProvider =

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

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

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文件的路径,即C:\ss\Class1.cs,您根据您的路径给出,但请记住给出一个小路径。

伙计们,您将这里的所有编辑都搞得一团糟。因此,简而言之,您想从C#编译一个
.cs
文件?那么什么不起作用了?@ShadowWizard,对不起,我没有看到结尾使用,我想OP忘记了a}@Adrian更恼人的是,由于与改进它的人合并,我变成了“我”(你是谁,哈哈……你错过了“我”的小型化)-不能责怪任何人,只是建议在跳上编辑马车之前等待一段时间。:)对于上面的代码,编译后.dll位于何处?它不工作,表示找不到指定的路径。第三行是什么意思。调试并检查它所使用的路径,并相应地更改路径。它将根据您的文件位置更改路径。。这你可以做对。。。我的机器现在没有windows,所以我无法执行并向您发送代码。。。我可以回家做那件事。。。为此你需要等待。。。但是,如果调试代码并进行相应更改,它将运行workProcess processCS=new Process();processCS.StartInfo.WorkingDirectory=@“C:\Documents and Settings\a511510\My Documents\Visual Studio 2010\Projects\tasks\wsdl1\wsdl1”;processCS.StartInfo.FileName=@“C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\csc.exe/target:library Program.cs”;processCS.StartInfo.CreateNoWindow=true;processCS.Start();processCS.WaitForExit();我的cs文件位于不同的目录中,我想编译它以获得相应的.dll。我不明白。你的代码名是什么。很明显,我有一个控制台程序,我按F5,我希望该程序编译另一个位于某个位置的.cs程序并生成.dll
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);