public List<EDSMSystem> GetSystemNames(bool resetLocal) { List<EDSMSystem> systemNames = new List<EDSMSystem>(); var localFile = new FileInfo(Path.Combine(Environment.CurrentDirectory, edsmDataFileName)); if (!resetLocal && localFile.Exists) { using (StreamReader r = new StreamReader(localFile.FullName)) { string json = r.ReadToEnd(); systemNames = JsonConvert.DeserializeObject<List<EDSMSystem>>(json); } } else { using (WebClient wc = new WebClient()) { var json = wc.DownloadString("http://www.edsm.net/api-v1/systems"); systemNames = JsonConvert.DeserializeObject<List<EDSMSystem>>(json); // save to local file. if (localFile.Exists) { localFile.Delete(true); } using ( StreamWriter outputFile = new StreamWriter(Path.Combine(Environment.CurrentDirectory.ToString(), edsmDataFileName))) { outputFile.WriteLine(JsonConvert.SerializeObject(systemNames)); } } } return systemNames; }
private static void UpgradeFile(string message, FileInfo keepFile, MovieConfiguration movie, FileInfo removeFile) { LOGGER.Info($"{message} remove {removeFile.FullName} as it is not as good quality than {keepFile.FullName}"); try { if (movie.ManualLocations.Contains(removeFile.DirectoryName)) { movie.ManualLocations.Remove(removeFile.DirectoryName); } removeFile.Delete(); if (removeFile.Directory.GetDirectories().Length > 0) { return; } if (removeFile.Directory.GetFiles().Any(f => f.IsMovieFile())) { return; } FileHelper.DoTidyUp(removeFile.Directory, TVSettings.Instance.Tidyup); } catch (FileNotFoundException) { //ignored} } catch (DirectoryNotFoundException) { //ignored} } catch (IOException) { //ignored} } }
private void FileInfo_Refresh(bool isNetwork) { using (var tempRoot = new TemporaryDirectory(isNetwork)) { var file = tempRoot.RandomTxtFileFullPath; var fiSysIo = new System.IO.FileInfo(file + "-System.IO.txt"); var fiAlphaFS = new Alphaleonis.Win32.Filesystem.FileInfo(file + "-AlphaFS.txt"); Console.WriteLine("System.IO Input File Path: [{0}]", fiSysIo.FullName); Console.WriteLine("AlphaFS Input File Path: [{0}]", fiAlphaFS.FullName); var existsSysIo = fiSysIo.Exists; var exists = fiAlphaFS.Exists; Assert.AreEqual(existsSysIo, exists); Assert.IsFalse(exists, "The file exists, but is expected not to."); var fsSysIo = fiSysIo.Create(); var fsAlphaFS = fiAlphaFS.Create(); existsSysIo = fiSysIo.Exists; exists = fiAlphaFS.Exists; Assert.AreEqual(existsSysIo, exists); Assert.IsFalse(exists, "The file exists, but is expected not to."); fiSysIo.Refresh(); fiAlphaFS.Refresh(); existsSysIo = fiSysIo.Exists; exists = fiAlphaFS.Exists; Assert.AreEqual(existsSysIo, exists); Assert.IsTrue(exists, "The file does not exists, but is expected to."); fsSysIo.Close(); fsAlphaFS.Close(); fiSysIo.Delete(); fiAlphaFS.Delete(); existsSysIo = fiSysIo.Exists; exists = fiAlphaFS.Exists; Assert.AreEqual(existsSysIo, exists); Assert.IsTrue(exists, "The file does not exists, but is expected to."); fiSysIo.Refresh(); fiAlphaFS.Refresh(); existsSysIo = fiSysIo.Exists; exists = fiAlphaFS.Exists; Assert.AreEqual(existsSysIo, exists); Assert.IsFalse(exists, "The file exists, but is expected not to."); } Console.WriteLine(); }
/// <summary> /// 删除文件 /// </summary> /// <param name="path"></param> public void DeleteFile(string path) { try { Alphaleonis.Win32.Filesystem.FileInfo fileInfo = new Alphaleonis.Win32.Filesystem.FileInfo(path); if (fileInfo.Exists) { fileInfo.Delete(); } } catch (System.Exception ex) { LogManager.WriteLog(LogFile.System, MessageUtil.GetExceptionMsg(ex, "")); } }
public static void SecureDeleteFile(FileInfo file, DeleteModes deleteMode) { if (deleteMode == DeleteModes.Simulate) { return; } if (deleteMode == DeleteModes.RecycleBin) { // Check CLR permissions -> could raise a exception new FileIOPermission(FileIOPermissionAccess.Write, file.FullName).Demand(); if (IsFileLocked(file)) { throw new REDPermissionDeniedException("Could not delete file \"" + file.FullName + "\" because the access is protected by the (file) system (permission denied)."); } FileSystem.DeleteFile(file.FullName, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin, UICancelOption.ThrowException); } else if (deleteMode == DeleteModes.RecycleBinShowErrors) { FileSystem.DeleteFile(file.FullName, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin, UICancelOption.ThrowException); } else if (deleteMode == DeleteModes.RecycleBinWithQuestion) { FileSystem.DeleteFile(file.FullName, UIOption.AllDialogs, RecycleOption.SendToRecycleBin, UICancelOption.ThrowException); } else if (deleteMode == DeleteModes.Direct) { // Was used for testing the error handling: // if (SystemFunctions.random.NextDouble() > 0.5) throw new Exception("Test error"); file.Delete(ignoreReadOnly: true); } else { throw new Exception("Internal error: Unknown delete mode: \"" + deleteMode + "\""); } }
public static async Task RemoveGivenFilesAsync(IEnumerable <string> FileList, List <string> DirectoryList = null, Definitions.List.TaskInfo CurrentTask = null) { try { if (CurrentTask != null) { await Task.Delay(5000); } Parallel.ForEach(FileList, currentFile => { FileInfo File = new FileInfo(currentFile); if (File.Exists) { if (CurrentTask != null) { CurrentTask.TaskStatusInfo = Framework.StringFormat.Format(SLM.Translate(nameof(Properties.Resources.TaskStatus_DeletingFile)), new { FileName = File.Name, FormattedFileSize = FormatBytes(File.Length) }); } Alphaleonis.Win32.Filesystem.File.SetAttributes(File.FullName, System.IO.FileAttributes.Normal); File.Delete(); } }); if (DirectoryList != null) { Parallel.ForEach(DirectoryList, currentDirectory => { DirectoryInfo Directory = new DirectoryInfo(currentDirectory); if (Directory.Exists) { if (CurrentTask != null) { CurrentTask.TaskStatusInfo = Framework.StringFormat.Format(SLM.Translate(nameof(Properties.Resources.TaskStatus_DeletingDirectory)), new { DirectoryName = Directory.Name }); } Directory.Delete(); } }); } if (CurrentTask != null) { CurrentTask.TaskStatusInfo = ""; } } catch (DirectoryNotFoundException ex) { logger.Error(ex); } catch (IOException ex) { logger.Error(ex); } catch (AggregateException ex) { logger.Error(ex); } catch (UnauthorizedAccessException ex) { logger.Error(ex); } catch (Exception ex) { logger.Fatal(ex); } }
private void FileInfo_Refresh(bool isNetwork) { UnitTestConstants.PrintUnitTestHeader(isNetwork); var tempPath = System.IO.Path.GetTempPath(); if (isNetwork) { tempPath = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(tempPath); } using (var rootDir = new TemporaryDirectory(tempPath, "FileInfo.Refresh")) { var file = rootDir.RandomFileFullPath; var fiSysIo = new System.IO.FileInfo(file + "-System.IO.txt"); var fiAlphaFS = new Alphaleonis.Win32.Filesystem.FileInfo(file + "-AlphaFS.txt"); Console.WriteLine("\nSystem.IO Input File Path: [{0}]", fiSysIo.FullName); Console.WriteLine("AlphaFS Input File Path: [{0}]", fiAlphaFS.FullName); var existsSysIo = fiSysIo.Exists; var exists = fiAlphaFS.Exists; Assert.AreEqual(existsSysIo, exists); Assert.IsFalse(exists, "The file exists, but is expected not to."); var fsSysIo = fiSysIo.Create(); var fsAlphaFS = fiAlphaFS.Create(); existsSysIo = fiSysIo.Exists; exists = fiAlphaFS.Exists; Assert.AreEqual(existsSysIo, exists); Assert.IsFalse(exists, "The file exists, but is expected not to."); fiSysIo.Refresh(); fiAlphaFS.Refresh(); existsSysIo = fiSysIo.Exists; exists = fiAlphaFS.Exists; Assert.AreEqual(existsSysIo, exists); Assert.IsTrue(exists, "The file does not exists, but is expected to."); fsSysIo.Close(); fsAlphaFS.Close(); fiSysIo.Delete(); fiAlphaFS.Delete(); existsSysIo = fiSysIo.Exists; exists = fiAlphaFS.Exists; Assert.AreEqual(existsSysIo, exists); Assert.IsTrue(exists, "The file does not exists, but is expected to."); fiSysIo.Refresh(); fiAlphaFS.Refresh(); existsSysIo = fiSysIo.Exists; exists = fiAlphaFS.Exists; Assert.AreEqual(existsSysIo, exists); Assert.IsFalse(exists, "The file exists, but is expected not to."); } Console.WriteLine(); }
public override void Delete() { instance.Delete(); }
private void DumpRefresh(bool isLocal) { #region Setup Console.WriteLine("\n=== TEST {0} ===", isLocal ? Local : Network); string tempPathSysIo = Path.GetTempPath("FileInfo.Refresh()-file-SysIo-" + Path.GetRandomFileName()); string tempPath = Path.GetTempPath("FileInfo.Refresh()-file-AlphaFS-" + Path.GetRandomFileName()); if (!isLocal) { tempPathSysIo = Path.LocalToUnc(tempPathSysIo); } if (!isLocal) { tempPath = Path.LocalToUnc(tempPath); } Console.WriteLine("\nInput File Path: [{0}]", tempPath); #endregion // Setup #region Refresh try { System.IO.FileInfo fiSysIo = new System.IO.FileInfo(tempPathSysIo); FileInfo fi = new FileInfo(tempPath); bool existsSysIo = fiSysIo.Exists; bool exists = fi.Exists; Console.WriteLine("\nnew FileInfo(): Exists (Should be {0}): [{1}]", existsSysIo, exists); // false Assert.AreEqual(existsSysIo, exists); FileStream fsSysIo = fiSysIo.Create(); FileStream fs = fi.Create(); existsSysIo = fiSysIo.Exists; exists = fi.Exists; Console.WriteLine("\nfi.Create(): Exists (Should be {0}): [{1}]", existsSysIo, exists); // false Assert.AreEqual(existsSysIo, exists); fiSysIo.Refresh(); fi.Refresh(); existsSysIo = fiSysIo.Exists; exists = fi.Exists; Console.WriteLine("\nfi.Refresh(): Exists (Should be {0}): [{1}]", existsSysIo, exists); // true Assert.AreEqual(existsSysIo, exists); fsSysIo.Close(); fs.Close(); existsSysIo = fiSysIo.Exists; exists = fi.Exists; Console.WriteLine("\nfi.Close(): Exists (Should be {0}): [{1}]", existsSysIo, exists); // true Assert.AreEqual(existsSysIo, exists); fiSysIo.Delete(); fi.Delete(); existsSysIo = fiSysIo.Exists; exists = fi.Exists; Console.WriteLine("\nfi.Delete(): Exists (Should be {0}): [{1}]", existsSysIo, exists); // true Assert.AreEqual(existsSysIo, exists); fiSysIo.Refresh(); fi.Refresh(); existsSysIo = fiSysIo.Exists; exists = fi.Exists; Console.WriteLine("\nfi.Refresh(): Exists (Should be False): [{0}]", exists); // false Assert.AreEqual(existsSysIo, exists); } finally { File.Delete(tempPath); Assert.IsFalse(File.Exists(tempPath), "Cleanup failed: File should have been removed."); File.Delete(tempPathSysIo); Assert.IsFalse(File.Exists(tempPath), "Cleanup failed: File should have been removed."); Console.WriteLine(); } #endregion // Refresh }
private List<EddbDataDTO> GetSystemsFromApi(bool resetLocalCache) { var systems = new List<EddbDataDTO>(); var localFile = new FileInfo(Path.Combine(Environment.CurrentDirectory, _eddbLocalCacheFileName)); if (!resetLocalCache && localFile.Exists) { using (StreamReader r = new StreamReader(localFile.FullName)) { string json = r.ReadToEnd(); systems = JsonConvert.DeserializeObject<List<EddbDataDTO>>(json); } } else { using (WebClient wc = new WebClient()) { var json = wc.DownloadString(_eddbSystemApiAddress.AbsoluteUri); systems = JsonConvert.DeserializeObject<List<EddbDataDTO>>(json); // save to local file. if (localFile.Exists) { localFile.Delete(true); } using ( StreamWriter outputFile = new StreamWriter(Path.Combine(Environment.CurrentDirectory.ToString(), _eddbLocalCacheFileName))) { outputFile.WriteLine(JsonConvert.SerializeObject(systems)); } } } return systems; }
private void DumpRefresh(bool isLocal) { #region Setup Console.WriteLine("\n=== TEST {0} ===", isLocal ? Local : Network); string tempPathSysIo = Path.GetTempPath("FileInfo.Refresh()-file-SysIo-" + Path.GetRandomFileName()); string tempPath = Path.GetTempPath("FileInfo.Refresh()-file-AlphaFS-" + Path.GetRandomFileName()); if (!isLocal) tempPathSysIo = Path.LocalToUnc(tempPathSysIo); if (!isLocal) tempPath = Path.LocalToUnc(tempPath); Console.WriteLine("\nInput File Path: [{0}]", tempPath); #endregion // Setup #region Refresh try { System.IO.FileInfo fiSysIo = new System.IO.FileInfo(tempPathSysIo); FileInfo fi = new FileInfo(tempPath); bool existsSysIo = fiSysIo.Exists; bool exists = fi.Exists; Console.WriteLine("\nnew FileInfo(): Exists (Should be {0}): [{1}]", existsSysIo, exists); // false Assert.AreEqual(existsSysIo, exists); FileStream fsSysIo = fiSysIo.Create(); FileStream fs = fi.Create(); existsSysIo = fiSysIo.Exists; exists = fi.Exists; Console.WriteLine("\nfi.Create(): Exists (Should be {0}): [{1}]", existsSysIo, exists); // false Assert.AreEqual(existsSysIo, exists); fiSysIo.Refresh(); fi.Refresh(); existsSysIo = fiSysIo.Exists; exists = fi.Exists; Console.WriteLine("\nfi.Refresh(): Exists (Should be {0}): [{1}]", existsSysIo, exists); // true Assert.AreEqual(existsSysIo, exists); fsSysIo.Close(); fs.Close(); existsSysIo = fiSysIo.Exists; exists = fi.Exists; Console.WriteLine("\nfi.Close(): Exists (Should be {0}): [{1}]", existsSysIo, exists); // true Assert.AreEqual(existsSysIo, exists); fiSysIo.Delete(); fi.Delete(); existsSysIo = fiSysIo.Exists; exists = fi.Exists; Console.WriteLine("\nfi.Delete(): Exists (Should be {0}): [{1}]", existsSysIo, exists); // true Assert.AreEqual(existsSysIo, exists); fiSysIo.Refresh(); fi.Refresh(); existsSysIo = fiSysIo.Exists; exists = fi.Exists; Console.WriteLine("\nfi.Refresh(): Exists (Should be False): [{0}]", exists); // false Assert.AreEqual(existsSysIo, exists); } finally { File.Delete(tempPath); Assert.IsFalse(File.Exists(tempPath), "Cleanup failed: File should have been removed."); File.Delete(tempPathSysIo); Assert.IsFalse(File.Exists(tempPath), "Cleanup failed: File should have been removed."); Console.WriteLine(); } #endregion // Refresh }