private void GenericCustomiseChooser(string title, string filename) { OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "Image files|*.jpeg;*.jpg;*.png;*.gif;*.bmp" + "|" + "All files|*.*"; dialog.CheckFileExists = true; dialog.Multiselect = false; dialog.Title = title; //dialog.FileName = filename; if (true == dialog.ShowDialog()) { // Copy the new file into place, if it is another file than the one we already have: filename = Path.GetFullPath(filename); string new_filename = Path.GetFullPath(dialog.FileName); if (0 != new_filename.CompareTo(filename)) { File.Delete(filename); File.Copy(new_filename, filename); } } else { File.Delete(filename); } UpdateLibraryStatistics(); }
public async Task <IActionResult> DeleteMyFile(string name) { var user = User.FindFirstValue(ClaimTypes.Name); Utils.Log($"Delete Uploaded File {user} {name}"); var files = await SQL.AllUploadedFilesForUser(name); var to_delete = files.First(f => f.MungedName == name); if (AlphaFile.Exists(Path.Combine("public", "files", to_delete.MungedName))) { AlphaFile.Delete(Path.Combine("public", "files", to_delete.MungedName)); } using (var client = new FtpClient("storage.bunnycdn.com")) { client.Credentials = new NetworkCredential(_settings.BunnyCDN_User, _settings.BunnyCDN_Password); await client.ConnectAsync(); if (await client.FileExistsAsync(to_delete.MungedName)) { await client.DeleteFileAsync(to_delete.MungedName); } } await SQL.DeleteUploadedFile(to_delete.Id); return(Ok($"Deleted {to_delete.MungedName}")); }
internal static void AlertUserAboutProblematicImports() { bool do_view = MessageBoxes.AskErrorQuestion("There were problems with some of the documents you were trying to add to Qiqqa. Do you want to see the problem details?", true); // do NOT spend a long time inside the lock! // hence we null the report file reference so that other threads can // create another report file and continue work while the user takes // a slow look at the old one... // // In short: take `Process.Start(...)` *outside* the lock! string report_filename = null; Utilities.LockPerfTimer l1_clk = Utilities.LockPerfChecker.Start(); lock (problematic_import_documents_lock) { l1_clk.LockPerfTimerStop(); if (do_view) { report_filename = problematic_import_documents_filename; } else { File.Delete(problematic_import_documents_filename); } // reset: problematic_import_documents_filename = null; problematic_import_documents_alert_showing = 0; } if (null != report_filename) { Process.Start(report_filename); } }
public async void UpdateLibraryPathAsync(string NewLibraryPath) { try { await Functions.Steam.CloseSteamAsync().ConfigureAwait(true); // Make a KeyValue reader var Key = new Framework.KeyValue(); // Read vdf file Key.ReadFileAsText(Global.Steam.VdfFilePath); // Change old library path with new one Key["Software"]["Valve"]["Steam"].Children.Find(key => key.Value.Contains(FullPath)).Value = NewLibraryPath; // Update config.vdf file with changes Key.SaveToFile(Global.Steam.VdfFilePath, false); // Since this file started to interrupt us? // No need to bother with it since config.vdf is the real deal, just remove it and Steam client will handle with some magic. if (File.Exists(Path.Combine(Properties.Settings.Default.steamInstallationPath, "steamapps", "libraryfolders.vdf"))) { File.Delete(Path.Combine(Properties.Settings.Default.steamInstallationPath, "steamapps", "libraryfolders.vdf")); } Functions.Steam.RestartSteamAsync(); } catch (Exception ex) { Logger.Fatal(ex); } }
private static void RotateCacheFiles([NotNull] FileInfo cacheFile) { if (cacheFile.Exists) { double hours = 999.9; if (File.Exists(cacheFile.FullName + ".0")) { // see when the last rotate was, and only rotate if its been at least an hour since the last save DateTime dt = File.GetLastWriteTime(cacheFile.FullName + ".0"); hours = DateTime.Now.Subtract(dt).TotalHours; } if (hours >= 24.0) // rotate the save file daily { for (int i = 8; i >= 0; i--) { string fn = cacheFile.FullName + "." + i; if (File.Exists(fn)) { string fn2 = cacheFile.FullName + "." + (i + 1); if (File.Exists(fn2)) { File.Delete(fn2); } File.Move(fn, fn2); } } File.Copy(cacheFile.FullName, cacheFile.FullName + ".0"); } } }
private void IgnoredItems_ButtonClick(object sender, RoutedEventArgs e) { try { foreach (var junk in Definitions.List.IgnoredJunkItems.ToList()) { switch ((string)(sender as Button)?.Tag) { case "remove": break; case "delete": if (File.Exists(junk)) { File.Delete(junk); } else if (Directory.Exists(junk)) { Directory.Delete(junk, true); } break; } Definitions.List.IgnoredJunkItems.Remove(junk); } } catch (Exception ex) { Logger.Fatal(ex); } }
public string ScrapeURLToFile(string url, bool force_download = false, Dictionary <string, string> additional_headers = null) { string cache_key = StreamMD5.FromText(url); string directory = Path.GetFullPath(Path.Combine(base_directory, cache_key.Substring(0, 2))); string filename = Path.GetFullPath(Path.Combine(directory, cache_key)); if (!File.Exists(filename) || force_download) { Utilities.Files.DirectoryTools.CreateDirectory(directory); // Crude throttle if (true) { while (DateTime.UtcNow.Subtract(last_scrape_time).TotalMilliseconds < throttle_ms) { Thread.Sleep(50); } last_scrape_time = DateTime.UtcNow; } Logging.Info("Downloading from {0}", url); using (WebClient client = new WebClient()) { if (null != additional_headers) { foreach (var pair in additional_headers) { client.Headers.Add(pair.Key, pair.Value); } } if (!String.IsNullOrEmpty(userAgent)) { client.Headers.Add("User-agent", userAgent); } string temp_filename = filename + ".tmp"; try { client.DownloadFile(url, temp_filename); } catch (WebException ex) { File.WriteAllText(temp_filename, ex.ToString()); } File.Delete(filename); File.Move(temp_filename, filename); } string filename_manifest = Path.GetFullPath(Path.Combine(base_directory, @"manifest.txt")); string manifest_line = String.Format("{0}\t{1}", cache_key, url); using (StreamWriter sw = File.AppendText(filename_manifest)) { sw.WriteLine(manifest_line); } } return(filename); }
public static void SaveRedundant(string filename, object animal_to_save) { string redundant_filename = filename + REDUNDANT; Save(redundant_filename, animal_to_save); File.Delete(filename); File.Move(redundant_filename, filename); }
// ------------------------------------------------------------------------------------------------------------------------------------------------------------------ // --- The equivalent protobuf versions // ------------------------------------------------------------------------------------------------------------------------------------------------------------------ #if !HAS_NO_PROTOBUF public static void ProtoSave <T>(string filename, T animal_to_save) { string redundant_filename = filename + REDUNDANT; ProtoSave_NotRedundant <T>(redundant_filename, animal_to_save); File.Delete(filename); File.Move(redundant_filename, filename); }
public static void ToJson <T>(this T obj, string filename) { if (File.Exists(filename)) { File.Delete(filename); } File.WriteAllText(filename, JsonConvert.SerializeObject(obj, Formatting.Indented, JsonSettings)); }
// ------------------------------------------------------------------------------------------------------------------------------------------------------------------ // --- The equivalent text versions // ------------------------------------------------------------------------------------------------------------------------------------------------------------------ public static void TextSave(string filename, string animal_to_save) { string redundant_filename = filename + REDUNDANT; TextSave_NotRedundant(redundant_filename, animal_to_save); File.Delete(filename); File.Move(redundant_filename, filename); }
public async Task TestUpdating() { var profile = utils.AddProfile(); var mod = utils.AddMod(); var unchanged = utils.AddModFile(mod, @"Data\scripts\unchanged.pex", 10); var deleted = utils.AddModFile(mod, @"Data\scripts\deleted.pex", 10); var modified = utils.AddModFile(mod, @"Data\scripts\modified.pex", 10); utils.Configure(); utils.AddManualDownload( new Dictionary <string, byte[]> { { "/baz/unchanged.pex", File.ReadAllBytes(unchanged) }, { "/baz/deleted.pex", File.ReadAllBytes(deleted) }, { "/baz/modified.pex", File.ReadAllBytes(modified) }, }); await CompileAndInstall(profile); utils.VerifyInstalledFile(mod, @"Data\scripts\unchanged.pex"); utils.VerifyInstalledFile(mod, @"Data\scripts\deleted.pex"); utils.VerifyInstalledFile(mod, @"Data\scripts\modified.pex"); var unchanged_path = utils.PathOfInstalledFile(mod, @"Data\scripts\unchanged.pex"); var deleted_path = utils.PathOfInstalledFile(mod, @"Data\scripts\deleted.pex"); var modified_path = utils.PathOfInstalledFile(mod, @"Data\scripts\modified.pex"); var extra_path = utils.PathOfInstalledFile(mod, @"something_i_made.foo"); File.WriteAllText(extra_path, "bleh"); var extra_folder = Path.Combine(Path.GetDirectoryName(utils.PathOfInstalledFile(mod, @"something_i_made.foo")), "folder_i_made"); Directory.CreateDirectory(extra_folder); Assert.IsTrue(Directory.Exists(extra_folder)); var unchanged_modified = File.GetLastWriteTime(unchanged_path); var modified_modified = File.GetLastWriteTime(modified_path); File.WriteAllText(modified_path, "random data"); File.Delete(deleted_path); Assert.IsTrue(File.Exists(extra_path)); await CompileAndInstall(profile); utils.VerifyInstalledFile(mod, @"Data\scripts\unchanged.pex"); utils.VerifyInstalledFile(mod, @"Data\scripts\deleted.pex"); utils.VerifyInstalledFile(mod, @"Data\scripts\modified.pex"); Assert.AreEqual(unchanged_modified, File.GetLastWriteTime(unchanged_path)); Assert.AreNotEqual(modified_modified, File.GetLastWriteTime(modified_path)); Assert.IsFalse(File.Exists(extra_path)); Assert.IsFalse(Directory.Exists(extra_folder)); }
internal string StorePageTextGroup(int page, string source_filename) { string filename = MakeFilename_TextGroup(page); Directory.CreateDirectory(Path.GetDirectoryName(filename)); File.Copy(source_filename, filename, true); File.Delete(source_filename); return(filename); }
internal void StorePageTextSingle(int page, string source_filename) { string filename = MakeFilename_TextSingle(page); Directory.CreateDirectory(Path.GetDirectoryName(filename)); File.Copy(source_filename, filename, true); File.Delete(source_filename); OnPageTextAvailable?.Invoke(page, page); }
static Utils() { var program_name = Assembly.GetEntryAssembly()?.Location ?? "Wabbajack"; LogFile = program_name + ".log"; _startTime = DateTime.Now; if (LogFile.FileExists()) { File.Delete(LogFile); } }
public void Build(string outputName) { RegenFolderRecords(); if (File.Exists(outputName)) { File.Delete(outputName); } using (var fs = File.OpenWrite(outputName)) using (var wtr = new BinaryWriter(fs)) { wtr.Write(_fileId); wtr.Write(_version); wtr.Write(_offset); wtr.Write(_archiveFlags); var folders = FolderNames.ToList(); wtr.Write((uint)folders.Count); wtr.Write((uint)_files.Count); wtr.Write((uint)_folders.Select(f => f._nameBytes.Count() - 1).Sum()); // totalFolderNameLength var s = _files.Select(f => f._pathBytes.Count()).Sum(); _totalFileNameLength = (uint)_files.Select(f => f._nameBytes.Count()).Sum(); wtr.Write(_totalFileNameLength); // totalFileNameLength wtr.Write(_fileFlags); foreach (var folder in _folders) { folder.WriteFolderRecord(wtr); } foreach (var folder in _folders) { if (HasFolderNames) { wtr.Write(folder._nameBytes); } foreach (var file in folder._files) { file.WriteFileRecord(wtr); } } foreach (var file in _files) { wtr.Write(file._nameBytes); } foreach (var file in _files) { file.WriteData(wtr); } } }
public override async void RemoveLibraryAsync(bool withFiles) { try { if (withFiles) { DeleteFilesAsync(); } List.Libraries.Remove(this); if (Type != LibraryType.Steam) { return; } await Functions.Steam.CloseSteamAsync().ConfigureAwait(true); // Make a KeyValue reader var keyValReader = new Framework.KeyValue(); // Read vdf file keyValReader.ReadFileAsText(Global.Steam.VdfFilePath); // Remove old library keyValReader["Software"]["Valve"]["Steam"].Children.RemoveAll(x => x.Value == FullPath); var i = 1; foreach (var key in keyValReader["Software"]["Valve"]["Steam"].Children.FindAll(x => x.Name.Contains("BaseInstallFolder"))) { key.Name = $"BaseInstallFolder_{i}"; i++; } // Update libraryFolders.vdf file with changes keyValReader.SaveToFile(Global.Steam.VdfFilePath, false); // Since this file started to interrupt us? // No need to bother with it since config.vdf is the real deal, just remove it and Steam client will handle with some magic. if (File.Exists(Path.Combine(Properties.Settings.Default.steamInstallationPath, "steamapps", "libraryfolders.vdf"))) { File.Delete(Path.Combine(Properties.Settings.Default.steamInstallationPath, "steamapps", "libraryfolders.vdf")); } Functions.Steam.RestartSteamAsync(); } catch (Exception ex) { Logger.Fatal(ex); } }
public static void Delete(string filename) { try { if (File.Exists(filename)) { File.Delete(filename); } } catch (Exception ex) { Logging.Warn(ex, "There was a problem deleting file {0}", filename); } }
public void ClearOCRText() { Logging.Info("Clearing OCR for document " + document_fingerprint); // Delete the OCR files for (int page = 1; page <= PageCount; ++page) { // First the SINGLE file { string filename = pdf_render_file_layer.MakeFilename_TextSingle(page); if (File.Exists(filename)) { try { File.Delete(filename); } catch (Exception ex) { Logging.Error(ex, "There was a problem while trying to delete the OCR file " + filename); } } } // Then the MULTI file { string filename = pdf_render_file_layer.MakeFilename_TextGroup(page); if (File.Exists(filename)) { try { File.Delete(filename); } catch (Exception ex) { Logging.Error(ex, "There was a problem while trying to delete the OCR file " + filename); } } } } // Clear out the old texts //Utilities.LockPerfTimer l1_clk = Utilities.LockPerfChecker.Start(); lock (texts_lock) { //l1_clk.LockPerfTimerStop(); texts.Clear(); } }
private static void IUnderstand(object obj) { try { string[] matching_files = Directory.GetFiles(ConfigurationManager.Instance.BaseDirectoryForQiqqa, PREAMBLE_FILENAME + @"*", SearchOption.TopDirectoryOnly); foreach (string filename in matching_files) { File.Delete(filename); } } catch (Exception ex) { Logging.Error(ex, "There was a problem tidying up checking for Dropbox."); } }
public static bool MoveSafelyWithOverwriting(string source, string target) { if (File.Exists(source)) { if (File.Exists(target)) { File.Delete(target); } File.Move(source, target); return(true); } return(false); }
/// <summary> /// Executed as part of the `Approver` logic in /// /// ``` /// public static void Verify(IApprovalApprover approver, IApprovalFailureReporter reporter) /// { /// if (approver.Approve()) /// { /// approver.CleanUpAfterSuccess(reporter); /// } /// else /// { /// approver.ReportFailure(reporter); /// /// if (reporter is IReporterWithApprovalPower power && power.ApprovedWhenReported()) /// ^^^^^^^^^^^^^^^^^^^^^^ /// { /// approver.CleanUpAfterSuccess(power); /// } /// else /// { /// approver.Fail(); /// } /// } /// } /// ``` /// </summary> /// <returns>Return `false` when NOT approved automatically after all; `true` when this code auto-approves the test output, /// e.g. when the `*.approved.*` reference file does not (yet) exist.</returns> bool IReporterWithApprovalPower.ApprovedWhenReported() { if (!File.Exists(received)) { return(false); } #if false File.Delete(this.approved); #endif if (File.Exists(approved)) { return(false); } File.Copy(received, approved); return(File.Exists(approved)); }
private void ButtonPurgeDeletedPDFs_Click(object sender, RoutedEventArgs e) { HashSet <string> filenames_to_keep = new HashSet <string>(); HashSet <string> filenames_to_delete = new HashSet <string>(); // Get all the active and deleted files foreach (var x in WebLibraryManager.Instance.WebLibraryDetails_All_IncludingDeleted) { Library library = x.library; List <PDFDocument> pdf_documents = library.PDFDocuments_IncludingDeleted; foreach (PDFDocument pdf_document in pdf_documents) { if (library.WebLibraryDetail.Deleted || pdf_document.Deleted) { if (pdf_document.DocumentExists) { filenames_to_delete.Add(pdf_document.DocumentPath); } } else { filenames_to_keep.Add(pdf_document.DocumentPath); } } } // Remove all the items that we want to keep filenames_to_delete.RemoveWhere(o => filenames_to_keep.Contains(o)); // Ask the user if (MessageBoxes.AskQuestion("Are you sure you want to purge {0} PDF files?", filenames_to_delete.Count)) { foreach (string filename in filenames_to_delete) { Logging.Info("Purging {0}", filename); try { File.Delete(filename); } catch (Exception ex) { Logging.Warn(ex, "There was a problem purging {0}", filename); } } } }
internal void StorePageTextGroup(int page, int TEXT_PAGES_PER_GROUP, string source_filename) { string filename = MakeFilename_TextGroup(page); Directory.CreateDirectory(Path.GetDirectoryName(filename)); File.Copy(source_filename, filename, true); File.Delete(source_filename); if (null != OnPageTextAvailable) { int page_range_start = ((page - 1) / TEXT_PAGES_PER_GROUP) * TEXT_PAGES_PER_GROUP + 1; int page_range_end = page_range_start + TEXT_PAGES_PER_GROUP - 1; page_range_end = Math.Min(page_range_end, PageCount); OnPageTextAvailable?.Invoke(page_range_start, page_range_end); } }
public void Dispose() { var exts = new [] { ".md", ".exe" }; Directory.Delete(Path.Combine(WorkingDirectory, ID), true); Profiles.Do(p => { foreach (var ext in exts) { var path = Path.Combine(Directory.GetCurrentDirectory(), p + ext); if (File.Exists(path)) { File.Delete(path); } } }); }
private void LoadFromDisk() { try { HashIndex = new Dictionary <string, IEnumerable <VirtualFile> >(); Utils.Log("Loading VFS Cache"); if (!File.Exists("vfs_cache.bin")) { return; } _files = new Dictionary <string, VirtualFile>(); try { using (var fs = File.OpenRead("vfs_cache.bin")) using (var br = new BinaryReader(fs)) { var magic = Encoding.ASCII.GetString(br.ReadBytes(Magic.Length)); if (magic != Magic || br.ReadUInt64() != FileVersion) { fs.Close(); File.Delete("vfs_cache.bin"); return; } while (true) { var fr = VirtualFile.Read(br); _files.Add(fr.FullPath, fr); } } } catch (EndOfStreamException) { } CleanDB(); } catch (Exception ex) { Utils.Log($"Purging cache due to {ex}"); File.Delete("vfs_cache.bson"); _files.Clear(); } }
public void SyncToDisk() { if (!_disableDiskCache) { Utils.Status("Syncing VFS Cache"); lock (this) { if (!_isDirty) { return; } try { _isSyncing = true; if (File.Exists("vfs_cache.bin_new")) { File.Delete("vfs_cache.bin_new"); } using (var fs = File.OpenWrite("vfs_cache.bin_new")) using (var bw = new BinaryWriter(fs)) { Utils.Log($"Syncing VFS to Disk: {_files.Count} entries"); foreach (var f in _files.Values) { f.Write(bw); } } if (File.Exists("vfs_cache.bin")) { File.Delete("vfs_cache.bin"); } File.Move("vfs_cache.bin_new", "vfs_cache.bin"); _isDirty = false; } finally { _isSyncing = false; } } } }
public async Task <IActionResult> CleanUploads() { var files = await SQL.AllUploadedFiles(); var seen = new HashSet <string>(); var duplicate = new List <UploadedFile>(); foreach (var file in files) { if (seen.Contains(file.Name)) { duplicate.Add(file); } else { seen.Add(file.Name); } } using (var client = new FtpClient("storage.bunnycdn.com")) { client.Credentials = new NetworkCredential(_settings.BunnyCDN_User, _settings.BunnyCDN_Password); await client.ConnectAsync(); foreach (var dup in duplicate) { var final_path = Path.Combine("public", "files", dup.MungedName); Utils.Log($"Cleaning upload {final_path}"); if (AlphaFile.Exists(final_path)) { AlphaFile.Delete(final_path); } if (await client.FileExistsAsync(dup.MungedName)) { await client.DeleteFileAsync(dup.MungedName); } await SQL.DeleteUploadedFile(dup.Id); } } return(Ok(new { Remain = seen.ToArray(), Deleted = duplicate.ToArray() }.ToJson())); }
public void SyncToDisk() { if (!_disableDiskCache) { Utils.Status("Syncing VFS Cache"); lock (this) { try { _isSyncing = true; if (File.Exists("vfs_cache.bin_new")) { File.Delete("vfs_cache.bin_new"); } using (var fs = File.OpenWrite("vfs_cache.bin_new")) using (var bw = new BinaryWriter(fs)) { bw.Write(Encoding.ASCII.GetBytes(Magic)); bw.Write(FileVersion); Utils.Log($"Syncing VFS to Disk: {_files.Count} entries"); foreach (var f in _files.Values) { f.Write(bw); } } if (File.Exists("vfs_cache.bin")) { File.Delete("vfs_cache.bin"); } File.Move("vfs_cache.bin_new", "vfs_cache.bin"); } finally { _isSyncing = false; } } } }
private async Task ExtractTrack(AbsolutePath source, AbsolutePath destFolder, Track track) { var process = new ProcessHelper { Path = FFMpegPath, Arguments = new object[] { "-threads", 1, "-i", source, "-ss", track.Start, "-t", track.End - track.Start, track.Name.RelativeTo(destFolder).WithExtension(WAVExtension) }, ThrowOnNonZeroExitCode = true }; var ffmpegLogs = process.Output.Where(arg => arg.Type == ProcessHelper.StreamType.Output) .ForEachAsync(val => { Utils.Status($"Extracting {track.Name} - {val.Line}"); }); await process.Start(); if (track.Format == Track.FormatEnum.WAV) { return; } process = new ProcessHelper() { Path = xWMAEncodePath, Arguments = new object[] { "-b", 192000, track.Name.RelativeTo(destFolder).WithExtension(WAVExtension), track.Name.RelativeTo(destFolder).WithExtension(XWMExtension) }, ThrowOnNonZeroExitCode = true }; var xwmLogs = process.Output.Where(arg => arg.Type == ProcessHelper.StreamType.Output) .ForEachAsync(val => { Utils.Status($"Encoding {track.Name} - {val.Line}"); }); await process.Start(); if (File.Exists($"{destFolder}\\{track.Name}.wav")) { File.Delete($"{destFolder}\\{track.Name}.wav"); } }