سؤال

إذا كنت تستخدم فئة CodeDomProvider في .NET بعد اكتمال التجميع، فلن تتمكن من حذف مجموعة الإخراج.أريد أن أكون قادرا على حذف مجموعة الإخراج.File.Delete إرجاع استثناء تم رفض الوصول. giveacodicetagpre.

تحرير كعمل حول أنا أستخدم منشئ ثابت على الفصل الذي يبحث عن مجلد TEMP وحذف التجميعات التي تم إنشاؤها مسبقا.

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top