public void MoveFileExceptions() { string file1 = Path.GetTempFileName(); string file2 = Path.GetTempFileName(); Assert.Throws <IOException>(() => OsUtils.MoveFile(file1, file2)); if (OsUtils.Windows()) { // Pretty much everything is valid in a path under Linux, so only test on Windows Assert.Throws <IOException>(() => OsUtils.MoveFile(file1, file2 + ":")); } Assert.Throws <DirectoryNotFoundException>( () => OsUtils.MoveFile(file1, Path.Combine(Path.GetTempPath(), Path.GetRandomFileName(), "filename"))); }
public static string MoveToSaveFolder(string formatString, Programme progInfo, Episode epInfo, string baseSavePath, string extension, string sourceFile) { string rootName = Path.Combine(baseSavePath, CreateSaveFileName(formatString, progInfo, epInfo)); // Make sure the save folder exists (to support subfolders in the save file name template) Directory.CreateDirectory(OsUtils.GetDirectoryName(rootName)); for (int diffNum = 0; ; diffNum++) { string savePath = rootName + (diffNum > 0 ? diffNum.ToString(CultureInfo.CurrentCulture) : string.Empty) + "." + extension; if (savePath == sourceFile) { // File is already named correctly, nothing to do return(savePath); } if (!File.Exists(savePath)) { try { OsUtils.MoveFile(sourceFile, savePath); } catch (IOException e) { // We only want to handle IOException itself as a // number of IOException subclasses are thrown for // other cases we don't want to handle if (e.GetType() == typeof(IOException)) { // Destination file created since File.Exists check continue; } throw; } return(savePath); } } }