-1
我正在嘗試編寫一個程序,該程序可以將用戶生成的.cs文件編譯爲一個庫供以後使用(實際上只是爲了防止重新編譯它們下次程序啓動時)。當我嘗試將它們編譯爲.dll文件時,我收到一條錯誤消息,指出該文件正在被另一個程序使用。使用CodeProvider將許多外部.cs文件編譯爲.dll
這裏是我的實現:
CompilerParameters params = new CompilerParameters();
params.GenerateExecutable = false;
params.GenerateInMemory = false;
params.OutputAssembly = "test.dll";
params.TreatWarningsAsErrors = true;
CSharpCodeProvider provider = new CSharpCodeProvider();
DirectoryInfo di = DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] files = di.GetFiles("*.cs", SearchOption.AllDirectories);
foreach(FileInfo fi in files)
{
CompilerResults cr = provider.CompileAssemblyFromFile(params, fi.FullName);
if(cr.Errors.Count > 0)
{
foreach(CompilerError ce in cr.Errors)
{
Console.WriteLine(ce.ToString());
}
}
}
的第一個文件編譯就好了。之後,每一個文件,不過,給我這個錯誤:
CS0016: Cannot write to output file "temp.dll" -- The process cannot access the file because it is being used by another process.
爲什麼給我這個錯誤?如果我打算將許多類添加到程序集中,是否需要以不同的方式調用編譯器?
感謝您的幫助。
我想,你需要改變params.OutputAssembly =「test.dll」參數爲每個獨立的CS。 – mkysoft