12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- using System;
- using System.IO;
- using System.Collections.Generic;
- class Program
- {
- static void Main()
- {
- string iniFilePath = "application.ini"; // 파일 경로를 적절히 수정하세요
- // INI 파일 읽기
- Dictionary<string, Dictionary<string, string>> iniData = ReadIniFile(iniFilePath);
- // INI 파일에서 값을 가져오기
- string server = iniData["Database"]["Server"];
- string username = iniData["Database"]["Username"];
- Console.WriteLine($"Server: {server}");
- Console.WriteLine($"Username: {username}");
- // INI 파일에 쓰기
- iniData["Application"]["LogLevel"] = "INFO"; // 값을 변경하거나 추가할 수 있습니다.
- WriteIniFile(iniFilePath, iniData);
- }
- static Dictionary<string, Dictionary<string, string>> ReadIniFile(string filePath)
- {
- var iniData = new Dictionary<string, Dictionary<string, string>>();
- string currentSection = null;
- foreach (string line in File.ReadLines(filePath))
- {
- if (line.StartsWith("[") && line.EndsWith("]"))
- {
- currentSection = line.Trim('[', ']');
- iniData[currentSection] = new Dictionary<string, string>();
- }
- else if (!string.IsNullOrWhiteSpace(currentSection))
- {
- string[] parts = line.Split('=');
- if (parts.Length == 2)
- {
- string key = parts[0].Trim();
- string value = parts[1].Trim();
- iniData[currentSection][key] = value;
- }
- }
- }
- return iniData;
- }
- static void WriteIniFile(string filePath, Dictionary<string, Dictionary<string, string>> iniData)
- {
- using (var writer = new StreamWriter(filePath))
- {
- foreach (var section in iniData)
- {
- writer.WriteLine($"[{section.Key}]");
- foreach (var kvp in section.Value)
- {
- writer.WriteLine($"{kvp.Key}={kvp.Value}");
- }
- }
- }
- }
- }
|