private static bool TryGetImageFromTempFolder(string resource_name, out Image res) { res = null; if (!option.EnableFileCache || temporary_folder_path == null) { return(false); } resource_name = FileNameHelper.FilterFileName(resource_name); resource_name = resource_name.EndsWith(".cache") ? resource_name : (resource_name + ".cache"); var file_path = Path.Combine(temporary_folder_path, resource_name); if (File.Exists(file_path)) { try { res = Image.FromFile(file_path); return(true); } catch (Exception e) { Log.Debug("Failed to load cache image from cache folder:" + e.Message); return(false); } } else { return(false); } }
private static void InitLogFile() { try { var log_dir = SettingManager.LoadSetting <GlobalSetting>().LogOutputDirectory; Directory.CreateDirectory(log_dir); LogFilePath = Path.Combine(log_dir, FileNameHelper.FilterFileName(DateTime.Now.ToString() + ".log", '-')); file_writer = File.AppendText(LogFilePath); file_writer.AutoFlush = true; } catch (Exception e) { Log.Error($"Can't create log file {LogFilePath} : {e.Message}"); LogFilePath = null; } }
private static bool TryGetImageFromDownloadFolder(string name, out Image res) { res = null; name = FileNameHelper.FilterFileName(name); var file_path = Path.Combine(SettingManager.LoadSetting <GlobalSetting>().DownloadPath, name); if (!File.Exists(file_path)) { return(false); } res = Image.FromFile(file_path); //todo return(false); }
static Log() { try { var log_dir = SettingManager.LoadSetting <GlobalSetting>().LogOutputDirectory; Directory.CreateDirectory(log_dir); LogFilePath = Path.Combine(log_dir, FileNameHelper.FilterFileName(DateTime.Now.ToString() + ".log", '-')); file_writer = File.AppendText(LogFilePath); file_writer.AutoFlush = true; } catch (Exception e) { Log.Error($"Can't create log file {LogFilePath} : {e.Message}"); LogFilePath = null; } #if !DEBUG enable_debug_output = SettingManager.LoadSetting <GlobalSetting>().EnableOutputDebugMessage; #else enable_debug_output = true; #endif var console_window_option = SettingManager.LoadSetting <GlobalSetting>().ShowOutputWindow; #if DEBUG //Disable log window in designer mode if (App.Current?.MainWindow == null || System.ComponentModel.DesignerProperties.GetIsInDesignMode(App.Current?.MainWindow)) { console_window_option = LogWindowShowOption.None; Info("Maybe in designer mode , hide console window."); } #endif var enable_show_console_window = console_window_option == LogWindowShowOption.None ? false : (console_window_option == LogWindowShowOption.Always ? true : enable_debug_output); if (enable_show_console_window) { ConsoleWindow.Show(); } Info($"log_file_path = {LogFilePath}"); }
private static void CacheImageResourceAsFile(string resource_name, Image obj) { if (!option.EnableFileCache || temporary_folder_path == null) { return; } Stream stream = null; resource_name = FileNameHelper.FilterFileName(resource_name); resource_name = resource_name.EndsWith(".cache") ? resource_name : (resource_name + ".cache"); var file_path = Path.Combine(temporary_folder_path, resource_name); try { if (!CheckAndDeleteCacheFile(obj, out stream)) { return; } var file_stream = File.OpenWrite(file_path); var buffer = new byte[1024]; int read = 0; do { read = stream.Read(buffer, 0, buffer.Length); file_stream.Write(buffer, 0, read); } while (read != 0); Log.Debug($"Saved cache file :{file_path}"); } catch (Exception e) { Log.Debug("Failed to cache image to cache folder:" + e.Message); } finally { stream?.Dispose(); } }
internal static void LoadSettingFile() { try { load = true; var config_path = Path.GetFullPath(CONFIG_FILE_PATH); if (!File.Exists(config_path)) { SaveSettingFileInternal();//create new deafult file Log.Info($"Created new deafult config file to {config_path}"); load_object = new JObject(); } else { try { using var reader = File.OpenText(config_path); load_object = JsonConvert.DeserializeObject(reader.ReadToEnd()) as JObject ?? new JObject(); Log.Info($"Loaded config file from {config_path}"); } catch (Exception e) { Directory.CreateDirectory(BACKUP_CONFIG_FILES_PATH); var backup_file_path = Path.Combine(BACKUP_CONFIG_FILES_PATH, $"{Path.GetFileNameWithoutExtension(config_path)}.{FileNameHelper.FilterFileName(DateTime.Now.ToString("yyyy_MM_dd HH_mm_ss_fff"))}.backup.json"); Log.Error($"loading settings failed:{e},backup current setting file for protecting.copy file from {config_path} to {backup_file_path}"); File.Copy(config_path, backup_file_path, true); throw e; } } } catch (Exception e) { ExceptionHelper.DebugThrow(e); load_object = new JObject(); } }