The .NET Framework defines a System.IO.Path.GetTempFileName
method, which guarantees that the temporary filename it generates will be unique. As far as I can tell though, although extremely unlikely, this filename could be identical to the name of a directory at the same path, meaning that I can't assume that by taking the name of that file, deleting it, and creating a directory of the same name, I'll have a directory with a unique name to any other directory. Whatsmore, I can't specify the path under which GetTempFileName
should create its temp file. There doesn't seem to be an equivalent function to GetTempFileName
for directories.
Is there a GetTempFileName
equivalent for creating a unique directory? If not, what's the best way to create a unique directory at a specified location (ie. I specify the path under which to create the unique directory)?
Find where your temp files are stored by pressing and holding the Windows button, and then hit R to bring up the Run dialogue box. Type temp and press Enter (or click OK) to open up the folder location and see your temp files. Hold Ctrl and click individual items to select them for cleanup.
Open your File Explorer (it's usually the first button on your desktop taskbar, looks like a folder). Go to the "This PC" section on the left, and then double-click your C: drive. On the Home tab at the top, click "New Folder" and name it "Temp".
GetTempFileName generates a filename for a temporary file and optionally creates it for you. Temporary files are used to store data for short periods of time on the hard drive. The full filename, including the path, is put into the string variable passed as lpTempFileName .
To create and use a temporary fileThe application opens the user-provided source text file by using CreateFile. The application retrieves a temporary file path and file name by using the GetTempPath and GetTempFileName functions, and then uses CreateFile to create the temporary file.
You could use Guid.NewGuid().ToString()
for that task.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With