Is it considered good/acceptable practice to save a file in the temporary directory?
Asked Answered
W

4

6

I am developing a WinForms application using C# 3.5. I have a requirement to save a file on a temporary basis. Let's just say, for arguments sake, that's it's for a short duration of time while the user is viewing a particular tab on the app. After the user navigates away from the tab I am free to delete this file. Each time the user navigates to the tab(which is typically only done once), the file will be created(using a GUID name).

To get to my question - is it considered good practice to save a file to the temp directory? I'll be using the following logic:

Path.GetTempFileName();

My intention would be to create the file and leave it without deleting it. I'm going to assume here that the Windows OS cleans up the temp directory at some interval based on % of available space remaining.

Note: I had considered using the IsolatedStorage option to create the file and manually delete the file when I was finished using it i.e. when the user navigates away from the tab. However, it's not going so well as I have a requirement to get the Absolute or Relative path to the file and this does not appear to be an straight-forward/safe chore when interacting with IsolatedStorage. My opinion is that it's just not designed to allow this.

Weise answered 16/1, 2013 at 19:53 Comment(4)
its fine, as long as you don't leave any confidential data in those filesApriorism
(1) You should definitely make an effort to clean up the file when you are done with it. It's acceptable if the file gets lets behind when your application crashes or is killed forcibly, but under normal circumstances the file should disappear by itself if at all possible. (2) If creating the file in a directory which is world writable (as temporary directories usually are), look up standard practices on secure temp file creation and follow them.Northeastward
@Northeastward Is temp really world writable? AFAIK each user has its own temp directory, and I'd be surprised if other users can write there.Ovum
To really answer the question we need to understand the 'why'. It may or may not be appropriate depending on what the intent is. Using the file system may not be good practice for somethings. It kinda comes down to if there is a much better way to do it. "Using the file system" is a perceived implementation for some underlying requirement.Tsuda
B
5

I write temp files quite frequently. In my humble opionion the key is to clean up after one self by deleting unneeded temp files.

Bronchia answered 16/1, 2013 at 19:59 Comment(1)
Is it possible to stop/prevent/deny "other" potential apps (or the OS deleting the file) whilst it's in use? It would not be desirable for it to be deleted immediately after it's created but just before it's read by my app!Weise
T
4

In my opinion, it's a better practice to actually delete the temporary files when you don't need them. Consider the following remarks from Path.GetTempFileName() Method:

The GetTempFileName method will raise an IOException if it is used to create more than 65535 files without deleting previous temporary files.

The GetTempFileName method will raise an IOException if no unique temporary file name is available. To resolve this error, delete all unneeded temporary files.

Also, you should beaware about the following hotfix for Windows 7 and Windows Server 2008 R2.

Tapia answered 16/1, 2013 at 20:14 Comment(0)
A
3

Creating temp files in the temp directory is fine. It is considered good practice to clean up any temporary file when you are done using it.

Remember that temp files shouldn't persist any data you need on a long term basis (defined as across user sessions). Exaples of data needed "long term" are user settings or a saved data file.

Akihito answered 16/1, 2013 at 20:7 Comment(0)
P
2

Go ahead and save there, but clean up when you're done (closing the program). Keeping them until the end also allows re-use.

Parasitic answered 16/1, 2013 at 20:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.