private void AppConfigRead() { while (true) { if (Directory.Exists(Path.GetDirectoryName(APIKeyPath))) { if (File.Exists(APIKeyPath)) { StreamReader reader; try { reader = new StreamReader(APIKeyPath); XmlSerializer xmlSerializer = new XmlSerializer(APIKeyInfo.GetType()); try { APIKeyInfo = (SOLMain.APIKeySetDoc)xmlSerializer.Deserialize(reader); reader.Close(); break; } catch (InvalidOperationException e) { reader.Close(); SOLMain.WriteLogline(e.Message, SOLMain.LogLevel.Error); SOLMain.WriteLogline("密钥配置文件错误,重新生成密钥配置文件", SOLMain.LogLevel.Error); MessageBox.Show("请先填写用于访问腾讯云API的密钥", "未找到密钥配置文件", MessageBoxButtons.OK, MessageBoxIcon.Information); OpenConnectionForm(); } } catch (Exception e) { SOLMain.WriteLogline("读取密钥XML时抛出异常:" + e.Message, SOLMain.LogLevel.Error); throw e; } } else { SOLMain.WriteLogline("未找到密钥配置文件"); MessageBox.Show("请先填写用于访问腾讯云API的密钥,并设置主密钥", "未找到密钥配置文件", MessageBoxButtons.OK, MessageBoxIcon.Information); OpenConnectionForm(); } } else { SOLMain.WriteLogline("配置文件目录未建立,开始建立目录"); Directory.CreateDirectory(Path.GetDirectoryName(APIKeyPath)); } } }
private void XmlInfoWirte() { try { StreamWriter writer = new StreamWriter(APIKeyPath); XmlSerializer xmlSerializer = new XmlSerializer(FullAPIKeySet.GetType()); xmlSerializer.Serialize(writer, FullAPIKeySet); writer.Flush(); writer.Close(); } catch (Exception e) { SOLMain.WriteLogline("写入地区XML时抛出异常:" + e.Message, SOLMain.LogLevel.Error); throw e; } }