148 lines
5.4 KiB
C#
148 lines
5.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace KssSmaPlaLib.Ini
|
|
{
|
|
public static class IniHelper
|
|
{
|
|
public const string INI_PATH = @".\Settings.ini";
|
|
|
|
public static List<IniDto> IniList = new List<IniDto>();
|
|
|
|
// Win32 API宣言
|
|
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
|
|
private static extern int GetPrivateProfileString(
|
|
string lpAppName,
|
|
string lpKeyName,
|
|
string lpDefault,
|
|
StringBuilder lpReturnedString,
|
|
int nSize,
|
|
string lpFileName);
|
|
|
|
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
|
|
private static extern bool WritePrivateProfileString(
|
|
string lpAppName,
|
|
string lpKeyName,
|
|
string lpString,
|
|
string lpFileName);
|
|
|
|
/// <summary>
|
|
/// INIファイルから値を読み込み(セクション+キー指定)
|
|
/// </summary>
|
|
public static string ReadValue(string iniPath, string section, string key, string defaultValue = "")
|
|
{
|
|
// もしINIリストにあったら、そこから返す
|
|
var value = IniList
|
|
.FirstOrDefault(m => m.Section == section && m.Key == key)
|
|
?.Value;
|
|
if (value != null) return (string)value;
|
|
|
|
if (string.IsNullOrEmpty(iniPath)) iniPath = INI_PATH;
|
|
var sb = new StringBuilder(1024);
|
|
GetPrivateProfileString(section, key, defaultValue, sb, sb.Capacity, iniPath);
|
|
var str = sb.ToString();
|
|
str = string.IsNullOrEmpty(str) ? defaultValue : str;
|
|
return str;
|
|
}
|
|
|
|
/// <summary>
|
|
/// bool値の読み込み
|
|
/// </summary>
|
|
/// <param name="iniPath"></param>
|
|
/// <param name="section"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="defaultValue"></param>
|
|
/// <returns></returns>
|
|
public static bool ReadBoolValue(string iniPath, string section, string key, bool defaultValue = false)
|
|
{
|
|
var str = ReadValue(iniPath, section, key, defaultValue.ToString());
|
|
var boolValue = bool.TryParse(str, out var b) ? b : defaultValue;
|
|
return boolValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 日付値の読み込み
|
|
/// </summary>
|
|
public static DateTime ReadDateValue(string iniPath, string section, string key, DateTime? defaultDate = null)
|
|
{
|
|
var defDate = defaultDate ?? DateTime.MinValue;
|
|
|
|
var str = ReadValue(iniPath, section, key, defDate.ToString("yyyy/MM/dd"));
|
|
var dateValue = DateTime.TryParse(str, out var d) ? d : defDate;
|
|
return dateValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// INIファイルに値を書き込み(セクション+キー+値指定)
|
|
/// </summary>
|
|
public static bool WriteValue(string iniPath, string section, string key, string value)
|
|
{
|
|
if (string.IsNullOrEmpty(iniPath)) iniPath = INI_PATH;
|
|
return WritePrivateProfileString(section, key, value, iniPath);
|
|
}
|
|
|
|
/// <summary>
|
|
/// bool値の書き込み
|
|
/// </summary>
|
|
/// <param name="iniPath"></param>
|
|
/// <param name="section"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="value"></param>
|
|
/// <returns></returns>
|
|
public static bool WriteBoolValue(string iniPath, string section, string key, bool value)
|
|
{
|
|
return WriteValue(iniPath, section, key, value.ToString());
|
|
}
|
|
|
|
/// <summary>
|
|
/// 日付値の書き込み
|
|
/// </summary>
|
|
/// <param name="iniPath"></param>
|
|
/// <param name="section"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="value"></param>
|
|
/// <returns></returns>
|
|
public static bool WriteDateValue(string iniPath, string section, string key, DateTime value)
|
|
{
|
|
return WriteValue(iniPath, section, key, value.ToString("yyyy/MM/dd"));
|
|
}
|
|
|
|
public static string OpenIni(string initialFolderPath = "")
|
|
{
|
|
using (var dlg = new OpenFileDialog())
|
|
{
|
|
dlg.Multiselect = false;
|
|
dlg.Title = "Settings.ini ファイルを選択してください。";
|
|
dlg.AddExtension = true;
|
|
dlg.CheckFileExists = true;
|
|
dlg.CheckPathExists = true;
|
|
dlg.RestoreDirectory = true;
|
|
dlg.Filter = "設定ファイル(*.ini)|*.ini";
|
|
if (!string.IsNullOrEmpty(initialFolderPath))
|
|
{
|
|
// 最初に見せるフォルダ(デスクトップなど)
|
|
dlg.InitialDirectory = initialFolderPath;
|
|
}
|
|
|
|
// ダイアログを表示して「OK」が押されたら処理
|
|
if (dlg.ShowDialog() == DialogResult.Cancel) return string.Empty;
|
|
|
|
// 選択されたファイルのパスを取得
|
|
return dlg.FileName;
|
|
}
|
|
}
|
|
|
|
public static object ReadValue(object value, string name, object iNI_KEY_ITAKUSAKI_MEI, string v)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
}
|