/// <summary> /// 从加密文件中读取连接字符串 /// </summary> public static void LoadConnectionStrings() { // <connectionStrings configSource="connections.config"></connectionStrings> AMS.Profile.IProfile profile = new AMS.Profile.Xml(IOHelper.GetDataDirectory() + "\\Dbs.dat"); string skey = profile.GetValue("ConnectionStringSetting", "Key", string.Empty); if (!string.IsNullOrEmpty(skey)) { string cryptedConnectionStrings = profile.GetValue("ConnectionStringSetting", "ConnectionString", string.Empty); if (!string.IsNullOrEmpty(cryptedConnectionStrings)) { string connectionStrings = Cryptographer.DecryptSymmetric(cryptedConnectionStrings, skey); if (!string.IsNullOrEmpty(connectionStrings)) { string tempConfigFileName = IOHelper.GetDataDirectory() + "\\temp.config"; System.IO.File.WriteAllText(tempConfigFileName, connectionStrings, Encoding.UTF8); ExeConfigurationFileMap configMap = new ExeConfigurationFileMap { ExeConfigFilename = tempConfigFileName }; Configuration externalConfig = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None); foreach (ConnectionStringSettings i in externalConfig.ConnectionStrings.ConnectionStrings) { SystemHelper.ChangeConnectionString(i.Name, i.ConnectionString, i.ProviderName); } System.IO.File.Delete(tempConfigFileName); } } } }
private void bindGameComboBox() { gameConfigs.Clear(); gameComboBox.DataSource = null; //add None as a default gameConfigs.Add(new GameConfiguration { Path = String.Empty, Title = "None" }); //add games from the Portable Games folder String[] filePaths = Directory.GetFiles(Global.PortableGamesFolderPath, "*.*") .Where(file => file.EndsWith(".exe") || file.EndsWith(".swf")) .ToArray(); foreach (String filePath in filePaths) { gameConfigs.Add(new GameConfiguration { Path = filePath, Title = Path.GetFileNameWithoutExtension(filePath) }); } //add games from the config file AMS.Profile.Xml profile = new AMS.Profile.Xml(Global.ConfigurationFilePath); using (profile.Buffer()) { String[] entries = profile.GetEntryNames("GamePaths"); foreach (String entry in entries) { String gamePath = (String)profile.GetValue("GamePaths", entry); //if game does not exist delete the entry from the config file if (!File.Exists(gamePath)) { profile.RemoveEntry("GamePaths", entry); continue; } gameConfigs.Add(new GameConfiguration { Path = gamePath, Title = Path.GetFileNameWithoutExtension(gamePath) }); } } //bind game combo box gameComboBox.ValueMember = "Path"; gameComboBox.DisplayMember = "Title"; gameComboBox.DataSource = gameConfigs; }
private bool readConfig() { try { Assembly asm; asm = Assembly.GetExecutingAssembly(); string path; path = asm.Location.ToString(); path = path.Remove(path.LastIndexOf("\\")); AMS.Profile.Xml xml = new AMS.Profile.Xml(path + "\\Config\\gate.config"); connStr = xml.GetValue("config", "conStr", ""); this.autoStartStopConfig = Convert.ToInt32(xml.GetValue("config", "autoStartStop", "0")); this.dataTimeStart = Convert.ToInt32(xml.GetValue("config", "dataTimeStart", "100000")); this.dataTimeEnd = Convert.ToInt32(xml.GetValue("config", "dataTimeEnd", "180000")); this.mainAccount = xml.GetValue("config", "main_account", ""); this.gDBHost = xml.GetValue("gate", "host", "localhost"); this.gDBPort = xml.GetValue("gate", "port", "4001"); this.gDBPass = xml.GetValue("gate", "pass", ""); this.mailHost = xml.GetValue("email", "host", ""); this.mailPort = xml.GetValue("email", "port", "25"); this.mailFrom = xml.GetValue("email", "from", ""); this.mailTo = xml.GetValue("email", "to", ""); this.mailGateName = xml.GetValue("email", "gate_name", ""); //код брокера в торговой системе, не на бирже this.brokerId = Convert.ToInt32(xml.GetValue("config", "broker_id", "1")); return true; } catch (Exception e) { MessageBox.Show("Ошибка парсинга конфига "+e.Message); return false; } }
private void bindGameComboBox() { gameConfigs.Clear(); gameComboBox.DataSource = null; //add None as a default gameConfigs.Add(new GameConfiguration { Path = String.Empty, Title = "None" }); //add games from the Portable Games folder String[] filePaths = Directory.GetFiles(Global.PortableGamesFolderPath, "*.*") .Where(file => file.EndsWith(".exe") || file.EndsWith(".swf")) .ToArray(); foreach (String filePath in filePaths) gameConfigs.Add(new GameConfiguration { Path = filePath, Title = Path.GetFileNameWithoutExtension(filePath) }); //add games from the config file AMS.Profile.Xml profile = new AMS.Profile.Xml(Global.ConfigurationFilePath); using (profile.Buffer()) { String[] entries = profile.GetEntryNames("GamePaths"); foreach (String entry in entries) { String gamePath = (String)profile.GetValue("GamePaths", entry); //if game does not exist delete the entry from the config file if (!File.Exists(gamePath)) { profile.RemoveEntry("GamePaths", entry); continue; } gameConfigs.Add(new GameConfiguration { Path = gamePath, Title = Path.GetFileNameWithoutExtension(gamePath) }); } } //bind game combo box gameComboBox.ValueMember = "Path"; gameComboBox.DisplayMember = "Title"; gameComboBox.DataSource = gameConfigs; }
private void UserGUI_Load(object sender, EventArgs e) { //install JoyToKey if it does not exist if (!File.Exists(Global.JoyToKeyFolderPath + "JoyToKey.exe")) { #if (DEBUG) String archivePath = @"..\..\JoyToKey.zip"; #else String archivePath = @"JoyToKey.zip"; #endif using (ZipFile zip = ZipFile.Read(archivePath)) { zip.ExtractAll(Global.CommonApplicationDataFolderPath, ExtractExistingFileAction.DoNotOverwrite); //the archive contains the folder JoyToKey } } //create a config file if it does not exist if (!File.Exists(Global.ConfigurationFilePath)) { XmlDocument configDoc = new XmlDocument(); configDoc.LoadXml("<?xml version=\"1.0\" encoding=\"UTF-8\" ?><configuration><section name=\"GamePaths\"></section></configuration>"); Directory.CreateDirectory(Path.GetDirectoryName(Global.ConfigurationFilePath)); configDoc.Save(Global.ConfigurationFilePath); } //create a portable games folder if it does not exist if (!Directory.Exists(Global.PortableGamesFolderPath)) Directory.CreateDirectory(Global.PortableGamesFolderPath); bindGameComboBox(); bindJoyToKeyComboBox(); //bind settings checkboxes from config file String pathOfLastGamePlayed; Boolean autostartCheckBoxValue; //see http://www.codeproject.com/Articles/5304/Read-Write-XML-files-Config-files-INI-files-or-the AMS.Profile.Xml profile = new AMS.Profile.Xml(Global.ConfigurationFilePath); using (profile.Buffer()) { pathOfLastGamePlayed = profile.GetValue("Settings", "PathOfLastGamePlayed", String.Empty); joyToKeyComboBox.SelectedItem = profile.GetValue("Settings", "PathOfLastJoyToKeyConfigUsed", String.Empty); arcadeModeCheckBox.Checked = profile.GetValue("Settings", "ArcadeMode", false); fullScreenCheckBox.Checked = profile.GetValue("Settings", "FullScreen", false); hideMouseCheckBox.Checked = profile.GetValue("Settings", "HideMouse", false); autostartCheckBoxValue = profile.GetValue("Settings", "AutoStart", false); } autostartCheckBox.Checked = autostartCheckBoxValue; //TODO: hackish, save all data on closing instead? //autostart last game played if (autostartCheckBox.Checked && arcadeModeCheckBox.Checked == true && pathOfLastGamePlayed != String.Empty) { //select last game gameComboBox.SelectedItem = gameConfigs.Single(g => g.Path == pathOfLastGamePlayed); //play playButton_Click(null, null); } }
/// <summary> /// 从加密文件中读取连接字符串 /// </summary> public static bool LoadConnectionStrings() { try { if (ConfigurationManager.ConnectionStrings[DataConnectionStringName] != null) { return(false); } // <connectionStrings configSource="connections.config"></connectionStrings> AMS.Profile.IProfile profile = new AMS.Profile.Xml(SystemDirectory.DataDirectory + "\\Dbs.dat"); string skey = profile.GetValue("ConnectionStringSetting", "Key", string.Empty); if (!string.IsNullOrEmpty(skey)) { string cryptedConnectionStrings = profile.GetValue("ConnectionStringSetting", "ConnectionString", string.Empty); if (!string.IsNullOrEmpty(cryptedConnectionStrings)) { string connectionStrings = Cryptographer.DecryptSymmetric(cryptedConnectionStrings, skey); if (!string.IsNullOrEmpty(connectionStrings)) { string tempConfigFileName = SystemDirectory.DataDirectory + "\\temp.config"; System.IO.File.WriteAllText(tempConfigFileName, connectionStrings, Encoding.UTF8); ExeConfigurationFileMap configMap = new ExeConfigurationFileMap { ExeConfigFilename = tempConfigFileName }; Configuration externalConfig = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None); Configuration exeConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); foreach (ConnectionStringSettings i in externalConfig.ConnectionStrings.ConnectionStrings) { if (exeConfig.ConnectionStrings.ConnectionStrings[i.Name] == null) { exeConfig.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(i.Name, i.ConnectionString, i.ProviderName)); } else { //the full name of the connection string can be found in the app.config file // in the "name" attribute of the connection string exeConfig.ConnectionStrings.ConnectionStrings[i.Name].ConnectionString = i.ConnectionString; exeConfig.ConnectionStrings.ConnectionStrings[i.Name].ProviderName = i.ProviderName; } } if (!exeConfig.ConnectionStrings.SectionInformation.IsProtected) { //exeConfig.ConnectionStrings.SectionInformation.UnprotectSection(); // Encrypt the section. exeConfig.ConnectionStrings.SectionInformation.ProtectSection("DataProtectionConfigurationProvider"); } exeConfig.Save(ConfigurationSaveMode.Full); ConfigurationManager.RefreshSection(exeConfig.ConnectionStrings.SectionInformation.Name); // re-init ConfigurationManager var constructorInfo = typeof(System.Configuration.ConfigurationManager).GetConstructor( System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic, null, new Type[0], null); constructorInfo.Invoke(null, null); System.IO.File.Delete(tempConfigFileName); } } } return(true); } catch (System.Configuration.ConfigurationErrorsException) { RemoveEncryptedData(); throw; } }
private void UserGUI_Load(object sender, EventArgs e) { //install JoyToKey if it does not exist if (!File.Exists(Global.JoyToKeyFolderPath + "JoyToKey.exe")) { #if (DEBUG) String archivePath = @"..\..\JoyToKey.zip"; #else String archivePath = @"JoyToKey.zip"; #endif using (ZipFile zip = ZipFile.Read(archivePath)) { zip.ExtractAll(Global.CommonApplicationDataFolderPath, ExtractExistingFileAction.DoNotOverwrite); //the archive contains the folder JoyToKey } } //create a config file if it does not exist if (!File.Exists(Global.ConfigurationFilePath)) { XmlDocument configDoc = new XmlDocument(); configDoc.LoadXml("<?xml version=\"1.0\" encoding=\"UTF-8\" ?><configuration><section name=\"GamePaths\"></section></configuration>"); Directory.CreateDirectory(Path.GetDirectoryName(Global.ConfigurationFilePath)); configDoc.Save(Global.ConfigurationFilePath); } //create a portable games folder if it does not exist if (!Directory.Exists(Global.PortableGamesFolderPath)) { Directory.CreateDirectory(Global.PortableGamesFolderPath); } bindGameComboBox(); bindJoyToKeyComboBox(); //bind settings checkboxes from config file String pathOfLastGamePlayed; Boolean autostartCheckBoxValue; //see http://www.codeproject.com/Articles/5304/Read-Write-XML-files-Config-files-INI-files-or-the AMS.Profile.Xml profile = new AMS.Profile.Xml(Global.ConfigurationFilePath); using (profile.Buffer()) { pathOfLastGamePlayed = profile.GetValue("Settings", "PathOfLastGamePlayed", String.Empty); joyToKeyComboBox.SelectedItem = profile.GetValue("Settings", "PathOfLastJoyToKeyConfigUsed", String.Empty); arcadeModeCheckBox.Checked = profile.GetValue("Settings", "ArcadeMode", false); fullScreenCheckBox.Checked = profile.GetValue("Settings", "FullScreen", false); hideMouseCheckBox.Checked = profile.GetValue("Settings", "HideMouse", false); autostartCheckBoxValue = profile.GetValue("Settings", "AutoStart", false); } autostartCheckBox.Checked = autostartCheckBoxValue; //TODO: hackish, save all data on closing instead? //autostart last game played if (autostartCheckBox.Checked && arcadeModeCheckBox.Checked == true && pathOfLastGamePlayed != String.Empty) { //select last game gameComboBox.SelectedItem = gameConfigs.Single(g => g.Path == pathOfLastGamePlayed); //play playButton_Click(null, null); } }
/// <summary> /// 从加密文件中读取连接字符串 /// </summary> public static bool LoadConnectionStrings() { try { if (ConfigurationManager.ConnectionStrings[DataConnectionStringName] != null) return false; // <connectionStrings configSource="connections.config"></connectionStrings> AMS.Profile.IProfile profile = new AMS.Profile.Xml(SystemDirectory.DataDirectory + "\\Dbs.dat"); string skey = profile.GetValue("ConnectionStringSetting", "Key", string.Empty); if (!string.IsNullOrEmpty(skey)) { string cryptedConnectionStrings = profile.GetValue("ConnectionStringSetting", "ConnectionString", string.Empty); if (!string.IsNullOrEmpty(cryptedConnectionStrings)) { string connectionStrings = Cryptographer.DecryptSymmetric(cryptedConnectionStrings, skey); if (!string.IsNullOrEmpty(connectionStrings)) { string tempConfigFileName = SystemDirectory.DataDirectory + "\\temp.config"; System.IO.File.WriteAllText(tempConfigFileName, connectionStrings, Encoding.UTF8); ExeConfigurationFileMap configMap = new ExeConfigurationFileMap { ExeConfigFilename = tempConfigFileName }; Configuration externalConfig = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None); Configuration exeConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); foreach (ConnectionStringSettings i in externalConfig.ConnectionStrings.ConnectionStrings) { if (exeConfig.ConnectionStrings.ConnectionStrings[i.Name] == null) { exeConfig.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(i.Name, i.ConnectionString, i.ProviderName)); } else { //the full name of the connection string can be found in the app.config file // in the "name" attribute of the connection string exeConfig.ConnectionStrings.ConnectionStrings[i.Name].ConnectionString = i.ConnectionString; exeConfig.ConnectionStrings.ConnectionStrings[i.Name].ProviderName = i.ProviderName; } } if (!exeConfig.ConnectionStrings.SectionInformation.IsProtected) { //exeConfig.ConnectionStrings.SectionInformation.UnprotectSection(); // Encrypt the section. exeConfig.ConnectionStrings.SectionInformation.ProtectSection("DataProtectionConfigurationProvider"); } exeConfig.Save(ConfigurationSaveMode.Full); ConfigurationManager.RefreshSection(exeConfig.ConnectionStrings.SectionInformation.Name); // re-init ConfigurationManager var constructorInfo = typeof(System.Configuration.ConfigurationManager).GetConstructor( System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic, null, new Type[0], null); constructorInfo.Invoke(null, null); System.IO.File.Delete(tempConfigFileName); } } } return true; } catch (System.Configuration.ConfigurationErrorsException) { RemoveEncryptedData(); throw; } }