Possible Duplicate:
How can I create a temp file with a specific extension with .net ?
It is possible to create a temporary file in .NET by calling
string fileName = System.IO.Path.GetTempFileName();
This will create a file with a .TMP extension in the temporary directory.
What if you specifically want it to have a different extension? For the sake of this example, lets say that I needed a file ending with .TR5.
The obvious (and buggy) solution is to call
string fileName = Path.ChangeExtension(Path.GetTempFileName(), "tr5"))
The problems here are:
Is there a straightforward and safe way to generate a temporary file with a specific file exension?
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.
Temporary files typically have a . TMP or . TEMP file extension, but any naming convention might be used.
A temp file can be created by directly running mktemp command. The file created can only be read and written by the file owner by default. To ensure the file is created successfully, there should be an OR operator to exit the script if the file fails to be created.
Please See: How can I create a temp file with a specific extension with .net ?
I don't know if there's a supported method for generating an extension other than .tmp, but this will give you a unique file:
string fileName = Path.ChangeExtension(Path.GetTempFileName(), Guid.NewGuid().ToString() + "tr5"))
Not an elegant solution and I'm not sure if you need to then retrieve based on specific extension, but you could still write a pattern even for this.
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