Anky
Anky

Reputation: 111

Delete Temp files generated by using CompileAssemblyFromSource in c# application

I have a C# application which generates temp files at C:\Users\xxxx\AppData\Local\Temp. These temp files are being generated as a result of using Microsoft.CSharp.CSharpCodeProvider().CompileAssemblyFromSource. For each dynamic assembly following files (.tmp, .out, .err, .dll, .pdb, .cmdline, .cs) are generated. I tried to use

result.TempFiles.KeepFiles = false;

but this is not deleting these temp files automatically. How can I get rid of these temp files before the end of c# application execution ?

Upvotes: 2

Views: 1996

Answers (2)

Amit
Amit

Reputation: 890

If you do not want those temporary files to be generated at all then use CompilerParameters.GenerateInMemory = true; before using/calling CompileAssemblyFromSource

Please refer https://msdn.microsoft.com/en-us/library/system.codedom.compiler.compilerparameters.generateinmemory(v=vs.110).aspx

Upvotes: 1

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149538

KeepFiles seems to only be a flag:

Each temporary file in the collection has an associated keep file flag that determines, on a per-file basis, whether that file is to be kept or deleted. Files are automatically kept or deleted on completion of the compilation based on their associated keep files value. However, after compilation is complete, files that were kept can be released by setting KeepFiles false and calling the Delete method. This will result in all files being deleted.

Will either need to call TempFileCollection.Delete or using TempFileCollection.Dispose():

When you're done using the collection:

result.TempFiles.Delete();

Or

result.TempFiles.Dispose();

Which are equivalent.

Upvotes: 1

Related Questions