public static string GetTempFileName(string extension = ".tmp") { string re = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tmp", Path.GetRandomFileName() + extension); // perma fix for album artwork tmp\*.dds 'Could not find file' error IOExtension.MakeDirectory(Path.GetDirectoryName(re)); return(re); }
public static string GetTempFileName(string extension = ".tmp") { // perma fix for album artwork tmp\*.dds 'Could not find file' error var tempFile = Path.Combine(Path.GetTempPath(), "tmp", Path.GetRandomFileName() + extension); // some user don't have admin access to AppDomain.CurrentDomain.BaseDirectory //var tmpFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tmp", Path.GetRandomFileName() + extension); IOExtension.MakeDirectory(Path.GetDirectoryName(tempFile)); return(tempFile); }
public static void MoveDirectory(string sourceDirName, string destDirName, bool overwrite = false) { #region Validation checks if (String.IsNullOrWhiteSpace(sourceDirName)) { throw new ArgumentNullException("sourceDirName", "The source directory cannot be null."); } if (String.IsNullOrWhiteSpace(destDirName)) { throw new ArgumentNullException("destDirName", "The destination directory cannot be null."); } sourceDirName = sourceDirName.Trim(); destDirName = destDirName.Trim(); char[] invalidChars = System.IO.Path.GetInvalidPathChars(); if (sourceDirName.Contains(invalidChars)) { throw new ArgumentException("The directory contains invalid path characters.", "sourceDirName"); } if (destDirName.Contains(invalidChars)) { throw new ArgumentException("The directory contains invalid path characters.", "destDirName"); } DirectoryInfo sourceDir = new DirectoryInfo(sourceDirName); DirectoryInfo destDir = new DirectoryInfo(destDirName); if (!sourceDir.Exists) { throw new DirectoryNotFoundException("The path specified by sourceDirName is invalid: " + sourceDirName); } if (!overwrite) { if (destDir.Exists) { throw new IOException("The path specified by destDirName already exists: " + destDirName); } } #endregion if (sourceDir.Root.Name.Equals(destDir.Root.Name, StringComparison.InvariantCultureIgnoreCase)) { if (overwrite) { DeleteDirectory(destDirName, true); } // make sure the destination subroot directory exists to avoid // throwing System.IO error 'Could not find a part of path' if (!Directory.Exists(Path.GetDirectoryName(destDirName))) { Directory.CreateDirectory(Path.GetDirectoryName(destDirName)); } Directory.Move(sourceDirName, destDirName); } else { if (overwrite) { DeleteDirectory(destDirName, true); } Directory.CreateDirectory(destDirName); FileInfo[] files = sourceDir.GetFiles(); foreach (FileInfo file in files) { string newPath = Path.Combine(destDirName, file.Name); file.CopyTo(newPath); } DirectoryInfo[] subDirs = sourceDir.GetDirectories(); foreach (DirectoryInfo subDir in subDirs) { string newPath = Path.Combine(destDirName, subDir.Name); IOExtension.MoveDirectory(subDir.FullName, newPath); } Directory.Delete(sourceDirName, true); } }