コード例 #1
0
ファイル: Ini.cs プロジェクト: TheRake66/Winuntu
        // ===============================================================================================================================



        // ==[FONCTION]===================================================================================================================
        // Fonction        : ReadKey
        // Description     : Lit une clé d'une section du fichier .ini
        // Argument(s)     : string pSection = Nom de la section ou se trouve la clé
        //                 : string pKey = Nom de la clé à lire
        // Créateur(s)     : BUSTOS Thibault(TheRake66)
        // Version         : 1.0
        // ===============================================================================================================================
        public string ReadKey(string pSection, string pKey)
        {
            string[] IniText;
            bool     IniInSection = false;

            try { IniText = File.ReadAllText(this.IniFile).Split(new char[] { '\n', '\r' }); }
            catch { return(""); }


            foreach (string IniLine in IniText)
            {
                if (IniLine != "")
                {
                    // Si la section est la bonne
                    if (IniLine == "[" + pSection + "]")
                    {
                        IniInSection = true;
                    }
                    // Si on sort de la section
                    else if (IniLine.First() == '[' && IniLine.Last() == ']')
                    {
                        IniInSection = false;
                    }
                    // Si dans la section et que y'a la bonne clé
                    else if (IniInSection && IniLine.Length > pKey.Length && IniLine.Substring(0, pKey.Length + 1) == pKey + "=")
                    {
                        return(IniLine.Substring(pKey.Length + 1));
                    }
                }
            }

            return("");
        }
コード例 #2
0
ファイル: Ini.cs プロジェクト: TheRake66/Winuntu
        // ===============================================================================================================================



        // ==[FONCTION]===================================================================================================================
        // Fonction        : RenameKey
        // Description     : Renomme une clé d'une section du fichier .ini
        // Argument(s)     : string pSection = Nom de la section ou se trouve la clé
        //                 : string pKey = Nom de la clé à renommer
        //                 : string pNewName = Nouveau nom
        // Créateur(s)     : BUSTOS Thibault(TheRake66)
        // Version         : 1.0
        // ===============================================================================================================================
        public void RenameKey(string pSection, string pKey, string pNewName)
        {
            string[] IniText;
            string   IniConvert   = "";
            bool     IniInSection = false;

            try { IniText = File.ReadAllText(this.IniFile).Split(new char[] { '\n', '\r' }); }
            catch { return; }

            foreach (string IniLine in IniText)
            {
                if (IniLine != "")
                {
                    // Si la section est la bonne
                    if (IniLine == "[" + pSection + "]")
                    {
                        IniConvert  += IniLine + "\n";
                        IniInSection = true;
                    }
                    // Si on sort de la section
                    else if (IniLine.First() == '[' && IniLine.Last() == ']')
                    {
                        IniConvert  += IniLine + "\n";
                        IniInSection = false;
                    }
                    // Si dans la section et que y'a la bonne clé
                    else if (IniInSection && IniLine.Length > pKey.Length && IniLine.Substring(0, pKey.Length + 1) == pKey + "=")
                    {
                        IniConvert += pNewName + "=" + IniLine.Substring(pKey.Length + 1) + "\n";
                    }
                    else
                    {
                        IniConvert += IniLine + "\n";
                    }
                }
            }

            try { File.WriteAllText(this.IniFile, IniConvert); }
            catch { }
        }
コード例 #3
0
ファイル: Ini.cs プロジェクト: TheRake66/Winuntu
        // ===============================================================================================================================



        // ==[FONCTION]===================================================================================================================
        // Fonction        : WriteKey
        // Description     : Écrit une clé d'une section du fichier .ini
        // Argument(s)     : string pSection = Nom de la section ou se trouve la clé
        //                 : string pKey = Nom de la clé à écrire
        //                 : string pValue = Valeur de la clé
        // Créateur(s)     : BUSTOS Thibault(TheRake66)
        // Version         : 1.0
        // ===============================================================================================================================
        public void WriteKey(string pSection, string pKey, string pValue)
        {
            string[] IniText;
            bool     IniInSection    = false;
            string   IniConvert      = "";
            bool     IniModif        = false;
            bool     IniFoundSection = false;

            // Créer le fichier si il existe pas
            try
            {
                if (!File.Exists(this.IniFile))
                {
                    FileStream CreateFile = File.Create(this.IniFile);
                    CreateFile.Close();
                }
            }
            catch { return; }

            try { IniText = File.ReadAllText(this.IniFile).Split(new char[] { '\n', '\r' }); }
            catch { return; }

            foreach (string IniLine in IniText)
            {
                if (IniLine != "")
                {
                    // Si la section est la bonne
                    if (IniLine == "[" + pSection + "]")
                    {
                        IniConvert     += IniLine + "\n";
                        IniInSection    = true;
                        IniFoundSection = true;
                    }
                    // Si on sort de la section
                    else if (IniLine.First() == '[' && IniLine.Last() == ']')
                    {
                        // Si on sort de la section sans avoir ecrit
                        if (IniInSection && !IniModif)
                        {
                            IniConvert += pKey + "=" + pValue + "\n";
                            IniModif    = true;
                        }

                        IniConvert  += IniLine + "\n";
                        IniInSection = false;
                    }
                    // Si dans la section et que y'a la bonne clé
                    else if (IniInSection && IniLine.Length > pKey.Length && IniLine.Substring(0, pKey.Length + 1) == pKey + "=")
                    {
                        IniConvert += pKey + "=" + pValue + "\n";
                        IniModif    = true;
                    }
                    else
                    {
                        IniConvert += IniLine + "\n";
                    }
                }
            }

            // Si la section n'existe pas
            if (!IniFoundSection)
            {
                IniConvert += "[" + pSection + "]\n";
            }
            // Si la clé existe pas
            if (!IniModif)
            {
                IniConvert += pKey + "=" + pValue + "\n";
            }

            try { File.WriteAllText(this.IniFile, IniConvert); }
            catch { }
        }