Domanda

Se si utilizza la classe CodeDomProvider in .NET dopo aver completato la compilazione, non è possibile eliminare il gruppo di uscita.Voglio essere in grado di eliminare il gruppo di uscita.File.Delete restituisce un'eccezione negativa di accesso.

string asmPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N") + ".exe");
string keyPath = "some path to a *.snk file that works";
// build compiler
CodeDomProvider dom = CodeDomProvider.CreateProvider("VisualBasic");
CompilerParameters cp = new CompilerParameters();
cp.TreatWarningsAsErrors = false;
cp.GenerateInMemory = false;
cp.GenerateExecutable = true;
cp.CompilerOptions = "/target:winexe /keyfile:\"" + keyPath + "\"";
cp.OutputAssembly = asmPath;
// add all the other assembly references
string netPath = @"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\";
cp.ReferencedAssemblies.Add(netPath + @"System.dll");
cp.ReferencedAssemblies.Add(netPath + @"System.Core.dll");
cp.ReferencedAssemblies.Add(netPath + @"System.Data.dll");
cp.ReferencedAssemblies.Add(netPath + @"System.Xml.dll");
CompilerResults cr = dom.CompileAssemblyFromSource(cp, new string[] { code });
if (cr.Errors.Count == 0)
{
    cr.TempFiles.Delete();
    dom.Dispose();
// do stuff
...
//
    File.Delete(asmPath); // fails here Access Denied
}
.

Modifica come un lavoro in giro Sto utilizzando un costruttore statico sulla classe che cerca la cartella Temp e cancella gli assemblati creati in precedenza.

È stato utile?

Soluzione

I suspect the problem is that the assembly is loaded (in memory), and has the output file open, just as any other assembly that is currently loaded will have a handle to its executable file.

It's also possible (although I consider it unlikely) that the dom instance or the CompilerResults instance has a reference to the opened file. Neither is IDisposable, so I suspect they're not holding the file open.

Possible solutions:

You can compile the assembly to memory. That is, don't output a file. That solves the immediate problem. However, you still have the problem that the compiled assembly remains in memory. There's no way to unload an assembly without tearing down the app domain.

You can create a separate app domain to do the compile, and then communicate the results back to your main app domain. You can then shut down the compiler app domain, which will remove the compiled assembly from memory and also release any file handles that are referencing the file you just created. Your main program can then delete the file in question.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top