public static CreationReturnInfo OpenRegKey(string name, bool writable, RegistryHive hive = RegistryHive.CurrentUser) { // we are building x86 binary for both x86 and x64, which will // cause problem when opening registry key // detect operating system instead of CPU if (string.IsNullOrEmpty(name)) { return(CreationReturnInfo.Create(null, false, 0x0030004)); } try { RegistryKey userKey = RegistryKey.OpenBaseKey(hive, Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32) .OpenSubKey(name, writable); return(CreationReturnInfo.Create(userKey)); } catch (ArgumentException ae) { return(CreationReturnInfo.Create(null, false, 0x0030005)); } catch (Exception e) { return(CreationReturnInfo.Create(null, false, 0x0030006)); } }
public static CreationReturnInfo CreateFromJson(string json) { try { Config config = JsonConvert.DeserializeObject <Config>(json); return(CreationReturnInfo.Create(config)); } catch (Exception e) { return(CreationReturnInfo.Create(null, false, 0x0010001, e.ToString())); } }
public static CreationReturnInfo ReadFile(string path) { if (!File.Exists(path)) { return(CreationReturnInfo.Create(null, false, 0x0030001)); } string result = null; try { result = File.ReadAllText(path, Encoding.UTF8); } catch (Exception e) { return(CreationReturnInfo.Create(null, false, 0x0030002)); } return(CreationReturnInfo.Create(result)); }
/// <summary> /// 加载配置信息 /// </summary> /// <returns></returns> private BasicReturnInfo LoadConfig() { // 检查文件是否存在 if (!File.Exists(HardCode.CONFIGFILE_PATH + Path.AltDirectorySeparatorChar + HardCode.CONFIGFILE_NAME)) { // 不存在 则 直接使用缺省配置 ConfigInfo = new Config(); // 写入文件 BasicReturnInfo writeFileResult = SaveConfig(); // ErrorDetial: 0x0010002, "配置文件不存在" // ErrorDetail: 0x0010003, "配置文件不存在" + "保存配置文件出错" return(BasicReturnInfo.Create(false, 0x0010002 | writeFileResult.Code)); } else { // 配置文件 存在 // 读取配置文件 CreationReturnInfo readFileResult = Utils.ReadFile( HardCode.CONFIGFILE_PATH + Path.AltDirectorySeparatorChar + HardCode.CONFIGFILE_NAME); if (!readFileResult) { // 读取失败 // 使用缺省配置 ConfigInfo = new Config(); // ErrorDetail: 0x0010004, "读取配置文件失败" return(BasicReturnInfo.Create(false, 0x0010004)); } else { // 读取 成功 // 解析 配置 CreationReturnInfo createConfigResult = Config.CreateFromJson(readFileResult.Instance as string); if (!createConfigResult) { // 解析 失败 // 使用缺省配置 ConfigInfo = new Config(); // ErrorDetial: 0x0010008: "解析配置失败" return(BasicReturnInfo.Create(false, 0x0010008)); } else { // 解析 成功 ConfigInfo = createConfigResult.Instance as Config; return(BasicReturnInfo.OK()); } } } }