In internet been read a lot of times about people dont want to use Path.GetTempFileName
Because they says that could return a Already Existing file or when it changes the File Extension appears a conflict because a File With this Extension Exist, to solve problem make a file based on a GUID, well, this Function solves that Problem: Iterates until find a inexistent file name with an specific extension.
VB.net
Public Shared Function GetTempFileName(ByVal extensionWithDot As String) As String
Dim tempFileName As String
Do
tempFileName = System.IO.Path.GetTempFileName
If extensionWithDot IsNot Nothing Then
tempFileName = tempFileName.Replace(System.IO.Path.GetExtension(tempFileName), extensionWithDot)
End If
Loop While System.IO.File.Exists(tempFileName)
Return tempFileName
End Function
C#:
public static string GetTempFileName(string extensionWithDot)
{
string tempFileName = null;
do {
tempFileName = System.IO.Path.GetTempFileName;
if (extensionWithDot != null) {
tempFileName = tempFileName.Replace(System.IO.Path.GetExtension(tempFileName), extensionWithDot);
}
} while (System.IO.File.Exists(tempFileName));
return tempFileName;
}
Note: I use argument extensionWithDot because System.IO.Path.GetExtension returns with dot.