C # config informations de profil d'exploitation

 

Les informations suivantes sont des fichiers de configuration Méthode de lecture assembly référencé System.Configuration

    publique  classe config 
    { 
        publique  statique  chaîne du jeton = « » ;
         publique  statique  chaîne utilisateur GetAppConfig = ( « l'utilisateur » ); 




        // fichier de configuration spécifié lecture 
        privée  statique  chaîne File = GetFilePath ();
         privé  statique  Chaîne GetFilePath () 
        { 
            retour du système. Windows.Forms.Application.ExecutablePath; 
        } 

        /// <summary>  
        /// retours * exe.config section fichier de configuration appSettings valeur de l' élément  
         ///</ Sommaire>  
        /// <param name = "strKey"> </ param>  
        /// <retour> </ retour>  
        publique  statique  Chaîne GetAppConfig ( chaîne strKey) 
        { 
            la config de configuration = ConfigurationManager.OpenExeConfiguration (fichier);
             retour config .AppSettings.Settings [strKey] .Value; 
        } 

        ///  <summary> 
        /// renvoie un nom de chaîne de connexion connectionName connexion de données string  
         ///  </ summary> 
        ///  <param name = "connectionName"> </ param> 
        ///  <returns> </ retour>
        publique  statique  chaîneGetConnectionStringsConfig ( chaîne du connectionName) 
        { 
            la config de configuration = ConfigurationManager.OpenExeConfiguration (fichier),
             retour config.ConnectionStrings.ConnectionStrings [le connectionName] .ConnectionString.ToString (); 
        } 


        /// <summary>   
        /// dans le fichier * exe.config appSettings section de configuration ajoute une paire valeur clé  
         /// </ summary>   
        /// <param name = "newKey"> </ param>   
        /// <param name = "newValue"> </ param>   
        publique  statique  vide UpdateAppConfig ( chaîne newKey,chaîne newValue) 
        {
            Config de configuration = ConfigurationManager.OpenExeConfiguration (fichier);
            bool exist = false ;
            foreach ( string clé dans config.AppSettings.Settings.AllKeys) 
            { 
                si (touche == newKey) 
                { 
                    exist = true ; 
                } 
            } 
            Si (elles existent) 
            { 
                config.AppSettings.Settings.Remove (newKey); 
            } 
            Config.AppSettings.Settings.Add (newKey, newValue);
            config.Save (ConfigurationSaveMode.Modified); 
            ConfigurationManager.RefreshSection ( " les appSettings " ); 
        } 


        /// <summary>  
        /// Mise à jour de la chaîne de connexion  
         /// </ summary>  
        /// <param name = "newName"> nom de la chaîne de connexion </ param>  
        /// <param name = "newConString"> chaîne de connexion SOMMAIRE </ param>  
        /// <param name = "newProviderName"> nom du fournisseur de données </ param>  
        publique  statique  vide UpdateConnectionStringsConfig ( chaîne newName,chaîne newConString, chaînenewProviderName) 
        { 
            la config de configuration = ConfigurationManager.OpenExeConfiguration (fichier); 

            BOOL exist = false ; // enregistrement existe déjà si la chaîne de connexion  
             // si vous souhaitez modifier la chaîne de connexion existante   
            IF (config.ConnectionStrings.ConnectionStrings [newName] =! null ) 
            { 
                exist = true ; 
            } 
            // si la chaîne de connexion existe déjà, supprimez - le premier   
            IF (exist) 
            { 
                config.ConnectionStrings.ConnectionStrings.Remove (newName); 
            } 
            //Une nouvelle instance d'une chaîne de connexion   
            ConnectionStringSettings MySettings = new new ConnectionStringSettings (newName, newConString, newProviderName);
             // ajouter au profil de la nouvelle chaîne de connexion.               Config.ConnectionStrings.ConnectionStrings.Add (MySettings);
             // Enregistrer le profil en changement               config.Save (ConfigurationSaveMode.Modified);
             // section de configuration force reload connectionStrings du fichier de configuration   
            ConfigurationManager.RefreshSection ( " connectionStrings " ); 
        } /// <Sommaire> /// modifier tous les services à la fin de system.serviceModel le point d'adresses IP
         /// ne changent que IP ne change pas le port
         ///
                




         
         </ summary> 
        ///  <param name = "serverIP"> 127.0.0.1 </ param> 
        publique  statique  vide UpdateServiceModelConfig ( chaîne serverIP) 
        { 
            config Configuration = ConfigurationManager.OpenExeConfiguration (fichier); 
            ConfigurationSectionGroup sec = config.SectionGroups [ " system.serviceModel " ]; 
            ServiceModelSectionGroup serviceModelSectionGroup = sec comme ServiceModelSectionGroup; 
            ClientSection clientSection = serviceModelSectionGroup.Client;
            foreach (point ChannelEndpointElement dans clientSection.Endpoints) 
            { 
                string pattern = @ » \ b \ d {1,3} \. \ d {1,3} \. \ d {1,3} \. \ d {1,3 } \ b " ;
                string Adresse = item.Address.ToString ();
                chaîne de remplacement = chaîne .Format ( " {0} " , serverIP); 
                adresse = Regex.Replace (adresse, motif, remplacement); 
                item.Address = nouveau Uri (adresse); 
            }
 
            Config.Save (ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection ( " system.serviceModel " ); 
        } 



        ///  <summary> 
        /// 加密 
         ///  </ summary> 
        ///  <param name = "expéditeur"> </ param> 
        ///  <param name = "e"> </ param> 
        publique  statique  vide de cryptage () 
        { 
            config de configuration = ConfigurationManager.OpenExeConfiguration (fichier); 
            Section ConfigurationSection = config.GetSection ( " connectionStrings " );
            si (section!&&! 
            { 
                // section.SectionInformation.ProtectSection ( "DataProtectionConfigurationProvider"); 
                section.SectionInformation.ProtectSection ( " RSAProtectedConfigurationProvider " ); 
                config.Save (); 
            } 

            ConfigurationSection Section1 = config.GetSection ( " appSettings " );
            si (Section1 =! null &&! section1.SectionInformation.IsProtected) 
            { 
                // section1.SectionInformation.ProtectSection ( "DataProtectionConfigurationProvider");
                section1.SectionInformation.ProtectSection ( " RSAProtectedConfigurationProvider " ); 
                config.Save (); 
            } 

        } 

        ///  <summary> 
        /// 解密 
         ///  </ summary> 
        publique  statique  vide Décrypter () 
        { 
            config Configuration = ConfigurationManager.OpenExeConfiguration (fichier); 
            Section ConfigurationSection = config.GetSection ( " connectionStrings " );
            si (section! = null &&section.SectionInformation.IsProtected) 
            { 
                section.SectionInformation.UnprotectSection (); 
                config.Save (); 
            } 
            ConfigurationSection Section1 = config.GetSection ( " appSettings " );
            si (! Section1 = null && section1.SectionInformation.IsProtected) 
            { 
                section1.SectionInformation.UnprotectSection (); 
                config.Save (); 
            } 
        } 



    }
ConfigHelper
 
Obtenez le profil actuellement actif
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile

Je suppose que tu aimes

Origine www.cnblogs.com/su-king/p/12576055.html
conseillé
Classement