/// <summary> /// Creates a file with unique temporary file name with a given extension in the specified folder. /// File is guaranteed to be unique. /// Extension may have an initial period. /// If folder is null, the temporary folder will be used. /// Caller must delete it when finished. /// May throw IOException. /// </summary> internal static string GetTemporaryFile(string directory, string extension, bool createFile = true) { ErrorUtilities.VerifyThrowArgumentLengthIfNotNull(directory, nameof(directory)); ErrorUtilities.VerifyThrowArgumentLength(extension, nameof(extension)); if (extension[0] != '.') { extension = '.' + extension; } try { directory ??= Path.GetTempPath(); Directory.CreateDirectory(directory); string file = Path.Combine(directory, $"tmp{Guid.NewGuid():N}{extension}"); ErrorUtilities.VerifyThrow(!FileSystems.Default.FileExists(file), "Guid should be unique"); if (createFile) { File.WriteAllText(file, string.Empty); } return(file); } catch (Exception ex) when(ExceptionHandling.IsIoRelatedException(ex)) { throw new IOException(ResourceUtilities.FormatResourceStringStripCodeAndKeyword("Shared.FailedCreatingTempFile", ex.Message), ex); } }
internal static string GetTemporaryFile(string directory, string extension) { ErrorUtilities.VerifyThrowArgumentLengthIfNotNull(directory, "directory"); ErrorUtilities.VerifyThrowArgumentLength(extension, "extension"); if ((int)extension[0] != 46) { extension = "." + extension; } string path; try { directory = directory ?? Path.GetTempPath(); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } path = Path.Combine(directory, "tmp" + Guid.NewGuid().ToString("N") + extension); ErrorUtilities.VerifyThrow(!File.Exists(path), "Guid should be unique"); File.WriteAllText(path, string.Empty); } catch (Exception ex) { if (ExceptionHandling.NotExpectedException(ex)) { throw; } else { throw new IOException(ResourceUtilities.FormatResourceString("Shared.FailedCreatingTempFile", (object)ex.Message), ex); } } return(path); }
/// <summary> /// Creates a file with unique temporary file name with a given extension in the specified folder. /// File is guaranteed to be unique. /// Extension may have an initial period. /// If folder is null, the temporary folder will be used. /// Caller must delete it when finished. /// May throw IOException. /// </summary> internal static string GetTemporaryFile(string directory, string extension, bool createFile = true) { ErrorUtilities.VerifyThrowArgumentLengthIfNotNull(directory, "directory"); ErrorUtilities.VerifyThrowArgumentLength(extension, "extension"); if (extension[0] != '.') { extension = '.' + extension; } try { directory = directory ?? Path.GetTempPath(); Directory.CreateDirectory(directory); string file = Path.Combine(directory, string.Format("tmp{0}{1}", Guid.NewGuid().ToString("N"), extension)); ErrorUtilities.VerifyThrow(!File.Exists(file), "Guid should be unique"); if (createFile) { File.WriteAllText(file, String.Empty); } return(file); } catch (Exception ex) when(ExceptionHandling.IsIoRelatedException(ex)) { throw new IOException(ResourceUtilities.FormatResourceString("Shared.FailedCreatingTempFile", ex.Message), ex); } }