c create temp file

Definition

Creates a uniquely named, zero-byte temporary file on disk and returns the full path of that file.

Returns

The full path of the temporary file.

Exceptions

An I/O error occurs, such as no unique temporary file name is available.

This method was unable to create a temporary file.

Remarks

This method creates a temporary file with a .TMP file extension. The temporary file is created within the user’s temporary folder, which is the path returned by the GetTempPath 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.

For a list of common I/O tasks, see Common I/O Tasks.

Security

FileIOPermission
for writing to the temporary directory. Associated enumeration: Write

Определение

Создает на диске временный пустой файл с уникальным именем и возвращает полный путь этого файла. Creates a uniquely named, zero-byte temporary file on disk and returns the full path of that file.

Возвраты

Полный путь к временному файлу. The full path of the temporary file.

Исключения

Возникает ошибка ввода-вывода, например, отсутствует уникальное имя временного файла. An I/O error occurs, such as no unique temporary file name is available.

Этот метод не смог создать временный файл. This method was unable to create a temporary file.

Комментарии

Этот метод создает временный файл с. Расширение файла TMP. This method creates a temporary file with a .TMP file extension. Временный файл создается во временной папке пользователя, который является путем, возвращаемым методом GetTempPath. The temporary file is created within the user’s temporary folder, which is the path returned by the GetTempPath method.

Метод GetTempFileName вызывает IOException, если он используется для создания более чем 65535 файлов без удаления предыдущих временных файлов. The GetTempFileName method will raise an IOException if it is used to create more than 65535 files without deleting previous temporary files.

Метод GetTempFileName вызывает IOException, если уникальное имя временного файла недоступно. The GetTempFileName method will raise an IOException if no unique temporary file name is available. Чтобы устранить эту ошибку, удалите все ненужные временные файлы. To resolve this error, delete all unneeded temporary files.

Список общих задач ввода-вывода см. в разделе Общие задачи ввода-вывода. For a list of common I/O tasks, see Common I/O Tasks.

Безопасность

FileIOPermission
для записи во временный каталог. for writing to the temporary directory. Связанное перечисление: Write Associated enumeration: Write

I need to generate a unique temporary file with a .csv extension.

What I do right now is

However, this doesn’t guarantee that my .csv file will be unique.

I know the chances I ever got a collision are very low (especially if you consider that I don’t delete the .tmp files), but this code doesn’t looks good to me.

Of course I could manually generate random file names until I eventually find a unique one (which shouldn’t be a problem), but I’m curious to know if others have found a nice way to deal with this problem.

17 Answers 17

Guaranteed to be (statistically) unique:

(To quote from the wiki article on the probabilty of a collision:

. one’s annual risk of being hit by a meteorite is estimated to be one chance in 17 billion [19], that means the probability is about 0.00000000006 (6 × 10−11), equivalent to the odds of creating a few tens of trillions of UUIDs in a year and having one duplicate. In other words, only after generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%. The probability of one duplicate would be about 50% if every person on earth owns 600 million UUIDs

Оцените статью