I need to copy a file to a tmp location in order to manipulate it. I need to ensure that I always can copy the file.
- Is the following code correct?
- Does it ensure that never try to copy to a location that already exists?
- Is there any chance of failure? (Should I catch any exceptions)
The code that I am using is the following:
string tmpFile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Thanks in advance.
Perhaps use
Path.GetTempFileNameinstead?That method may throw
IOExceptionso you should probably catch that at some point. Exactly where depends more on the context in which this code exists.Note that this method also creates the file on disk, so there is no risk that some other process creates an identically named file between this call and the time when the file is written to by your code.