using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 智能仓储WCS管理系统 { /// /// 本地文件 /// public class LocalFile { /// /// 程序运行名称(智能仓储WCS管理系统.exe) /// public static readonly string AppName = AppDomain.CurrentDomain.FriendlyName.Contains('.') ? AppDomain.CurrentDomain.FriendlyName : $"{AppDomain.CurrentDomain.FriendlyName}.exe";//多环境兼容性 /// /// 程序运行目录 /// public static readonly string AppDir = AppDomain.CurrentDomain.BaseDirectory; /// /// 数据目录 /// public static readonly string DataDir = Path.Combine(AppDir, "data"); /// /// 日志目录 /// public static readonly string LogDir = Path.Combine(AppDir, "logs"); /// /// 资源目录 /// public static readonly string ResourcesDir = Path.Combine(AppDir, "Resources"); /// /// 运行主程序 /// public static readonly string AppPath = Path.Combine(AppDir, AppName); /// /// 配置文件路径 /// public static readonly string ConfigPath = Path.Combine(DataDir, "jsconfig.json"); /// /// 设备手动点位文档路径 /// public static readonly string PlcDotPath = Path.Combine(DataDir, "设备手动点位.xlsx"); /// /// 帮助文档路径 /// public static readonly string DocPath = Path.Combine(DataDir, "操作说明书.docx"); /// /// 打印模板 /// public static readonly string PrintTemplatePath = Path.Combine(ResourcesDir, "物料条码.btw"); static object lockConfig = new object(); static JsConfig config; /// /// 配置信息 /// public static JsConfig Config { get { if (config != null) return config; else { try { UpdateConfig(); } catch (Exception) { config = null; } } return config ?? new JsConfig(); } } /// /// 刷新配置信息。将本地的配置信息重新加载到内存中 /// public static void UpdateConfig() { if (File.Exists(ConfigPath)) lock (lockConfig) config = JsonConvert.DeserializeObject(File.ReadAllText(ConfigPath, Encoding.UTF8)); else config = null; } /// /// 保存配置信息。将内存中的配置信息保存到本地 /// public static void SaveConfig() { try { lock (lockConfig) File.WriteAllText(ConfigPath, JsonConvert.SerializeObject(Config, Formatting.Indented), Encoding.UTF8); } catch (Exception ex) { UpdateConfig(); throw ex; } } } }