Should Dispose() or Finalize() be used to delete temporary files?

后端 未结 8 1203
隐瞒了意图╮
隐瞒了意图╮ 2021-02-04 02:35

I have a class that makes use of temporary files (Path.GetTempFileName()) while it is active. I want to make sure these files do not remain on the user\'s hard driv

相关标签:
8条回答
  • 2021-02-04 03:11

    You should definitely use Dispose to clean up resources, but make sure you implement the IDisposable interface. You don't want to just add a method named Dispose.

    0 讨论(0)
  • 2021-02-04 03:16

    A file is an unmanaged resource, and you implement IDisposable to clean up unmanaged resources that your classes are dependent upon.

    I have implemented similar classes, although never in production code.

    However, I understand your tentativeness about this - user interaction with the files outside of your application could screw things up and cause problems during disposal. However, that is the same for any file created/deleted by an application, regardless of whether or not it's tidied up by a Dispose() method or not.

    I'd have to say that implementing IDisposable would be a reasonable choice.

    0 讨论(0)
提交回复
热议问题