Runtime: API Proposal: Path.GetTempDirectory

Created on 21 Oct 2019  路  5Comments  路  Source: dotnet/runtime

When dealing with temporary files, especially with more than one, we would like to create our own folder and place them inside. There are several ways to achieve this but for convenience and for completion of the existing Path.GetTempFileName there should be an official API for this.

The API name can be different (CreateTempDirectory?) to avoid confusion between GetTempPath and the meaning of it.

c# public static partial class Path { /// <summary> /// Creates a uniquely named, empty folder on disk and returns the full path of that directory. /// </summary> public static string GetTempDirectory() }

api-suggestion area-System.IO

Most helpful comment

dotnet/runtime#23538 and dotnet/runtime#23068 track similar requests (there may be others).

IMHO, what we need is a more official, general purpose TempFileCollection that lives in System.IO that allows you to:

  1. Create a temp directory that you can clean via disposal.
  2. Generate configurable filenames.
  3. Generate temp FileStream instances (which is more dependable than taking creating your own from a string, and it is also encourages writing more secure code).
  4. Potentially have the ability to clean orphaned temp directories (say, after an app crash). I've written a few of these, it is just might not be worth complicating the API here to do so. (Might be better as a second class.)

We _could_ extend TempFileCollection, but it is purpose built for CodeDom and isn't very discoverable. I'm not very keen on pushing that one forward.

All 5 comments

What format should the directory name have? A guid would be easy, but makes for a long path. For GetTempFileName, Windows since very old days used the tmpXXXX.tmp pattern (and it can run out if they leak) and it looks like for Linux, we emulate that with tmpXXXXXX.tmp

cc: @JeremyKuhne @carlossanlop for insights in this proposal

dotnet/runtime#23538 and dotnet/runtime#23068 track similar requests (there may be others).

IMHO, what we need is a more official, general purpose TempFileCollection that lives in System.IO that allows you to:

  1. Create a temp directory that you can clean via disposal.
  2. Generate configurable filenames.
  3. Generate temp FileStream instances (which is more dependable than taking creating your own from a string, and it is also encourages writing more secure code).
  4. Potentially have the ability to clean orphaned temp directories (say, after an app crash). I've written a few of these, it is just might not be worth complicating the API here to do so. (Might be better as a second class.)

We _could_ extend TempFileCollection, but it is purpose built for CodeDom and isn't very discoverable. I'm not very keen on pushing that one forward.

Also dotnet/runtime#25173 mentions this.

Closing as we intend to cover this request in dotnet/runtime#2048.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Timovzl picture Timovzl  路  3Comments

jchannon picture jchannon  路  3Comments

GitAntoinee picture GitAntoinee  路  3Comments

jzabroski picture jzabroski  路  3Comments

yahorsi picture yahorsi  路  3Comments