删除在C#应用程序中使用CompileAssemblyFromSource生成的Temp文件

Delete Temp files generated by using CompileAssemblyFromSource in c# application
2020-10-18
  •  译文(汉语)
  •  原文(英语)

我有一个C#应用程序,它在生成临时文件C:\Users\xxxx\AppData\Local\Temp.这些临时文件是使用生成的Microsoft.CSharp.CSharpCodeProvider().CompileAssemblyFromSource.对于每个动态程序集,都会生成以下文件(.tmp,.out,.err,.dll,.pdb,.cmdline和.cs).我尝试使用

result.TempFiles.KeepFiles = false;

但这不是自动删除这些临时文件.如何在c#应用程序执行结束之前摆脱这些临时文件?

解决过程1

KeepFiles似乎只是一个标志:

集合中的每个临时文件都有一个关联的保留文件标志,该标志根据文件确定要保留还是删除该文件.根据文件的关联保留文件值,在完成编译后自动保留或删除文件.但是,在编译完成之后,可以通过将KeepFiles设置为false并调用Delete方法来释放保留的文件.这将导致所有文件被删除.

将需要致电TempFileCollection.Delete或使用TempFileCollection.Dispose():

使用完集合后:

result.TempFiles.Delete();

要么

result.TempFiles.Dispose();

这是等效的.

速聊1:
result.TempFiles.KeepFiles = false;在语句后标记该标志result = new Microsoft.CSharp.CSharpCodeProvider().CompileAssemblyFromSource,然后使用result.TempFiles.Dispose(); CompileAssemblyFromSource之前应该引发KeepFiles标志吗?
速聊2:
KeepFiles在调用之前应该为false,Delete这样它就知道不保留任何文件.
解决过程2

如果您根本不希望生成这些临时文件,请CompilerParameters.GenerateInMemory = true;在使用/调用之前使用CompileAssemblyFromSource

请参考https://msdn.microsoft.com/zh-cn/library/system.codedom.compiler.compilerparameters.generateinmemory(v=vs.110).aspx

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 ?

Solutions1

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.

Talk1:
I am marking the flag result.TempFiles.KeepFiles = false; after the statement result = new Microsoft.CSharp.CSharpCodeProvider().CompileAssemblyFromSource and then using result.TempFiles.Dispose(); Should KeepFiles flag be raised before using CompileAssemblyFromSource ?
Talk2:
KeepFiles should be false before you invoke Delete so it knows not to keep any files.
Solutions2

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

转载于:https://stackoverflow.com/questions/30773707/delete-temp-files-generated-by-using-compileassemblyfromsource-in-c-sharp-applic

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入