Setting.ini
[Database]
Server=localhost
Port=3306
Database=mydatabase
User=root
Password=123456
C# kodu
using System;
using System.Collections.Generic;
using System.IO;
class Program
{
static void Main(string[] args)
{
string iniFilePath = “settings.ini”; // .ini dosyasını okuma Dictionary<string, Dictionary<string, string>> settings = ReadIniFile(iniFilePath); // Örnek: Database bölümündeki ayarları gösterme if (settings.ContainsKey("Database")) { Dictionary<string, string> databaseSettings = settings["Database"]; foreach (var kvp in databaseSettings) { Console.WriteLine($"{kvp.Key}: {kvp.Value}"); } } // .ini dosyasına yeni ayar ekleme AddOrUpdateIniSetting(iniFilePath, "Database", "Password", "newpassword"); } static Dictionary<string, Dictionary<string, string>> ReadIniFile(string filePath) { var settings = new Dictionary<string, Dictionary<string, string>>(); string currentSection = null; foreach (string line in File.ReadLines(filePath)) { if (line.StartsWith("[") && line.EndsWith("]")) { currentSection = line.Substring(1, line.Length - 2); settings[currentSection] = new Dictionary<string, string>(); } else if (!string.IsNullOrWhiteSpace(line) && line.Contains("=") && currentSection != null) { string[] parts = line.Split(new char[] { '=' }, 2); settings[currentSection][parts[0].Trim()] = parts[1].Trim(); } } return settings; } static void AddOrUpdateIniSetting(string filePath, string section, string key, string value) { var settings = ReadIniFile(filePath); if (settings.ContainsKey(section)) { settings[section][key] = value; } else { settings[section] = new Dictionary<string, string> { { key, value } }; } WriteIniFile(filePath, settings); } static void WriteIniFile(string filePath, Dictionary<string, Dictionary<string, string>> settings) { using (StreamWriter writer = new StreamWriter(filePath)) { foreach (var section in settings) { writer.WriteLine($"[{section.Key}]"); foreach (var kvp in section.Value) { writer.WriteLine($"{kvp.Key}={kvp.Value}"); } writer.WriteLine(); } } }
}
İlk Yorumu Siz Yapın