using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Runtime.InteropServices; namespace INI.IniFiles { /* 参考:INIファイルを読み書きするには? * https://www.atmarkit.co.jp/fdotnet/dotnettips/039inifile/inifile.html */ class IniFiles { [DllImport("KERNEL32.DLL")] private static extern uint GetPrivateProfileString( string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, uint nSize, string lpFileName); [DllImport("KERNEL32.DLL")] private static extern uint GetPrivateProfileInt( string lpAppName, string lpKeyName, int nDefault, string lpFileName); [DllImport("KERNEL32.DLL", EntryPoint = "GetPrivateProfileStringA")] public static extern uint GetPrivateProfileStringByByteArray( string lpAppName, string lpKeyName, string lpDefault, byte[] lpReturnedString, uint nSize, string lpFileName ); [DllImport("KERNEL32.DLL")] private static extern uint WritePrivateProfileString( string lpAppName, string lpKeyName, string lpString, string lpFileName); //******************************************* // GetIniString // //******************************************* /// <summary> /// INIファイルから値を取得する /// </summary> /// <param name="lpSection">セッション名称 /// <param name="lpKeyName">キー名称 /// <param name="lpFileName">INIファイル名 /// <returns></returns> public static string GetIniString(string lpSection, string lpKeyName, string lpFileName) { System.Text.StringBuilder sb = new System.Text.StringBuilder(1024); uint sLen = GetPrivateProfileString(lpSection, lpKeyName, "", sb, (uint)sb.Capacity, lpFileName); return sb.ToString(); } //******************************************* // GetPrivateProfileInt // //******************************************* /// <summary> /// /// </summary> /// <param name="lpSection"> /// <param name="lpKeyName"> /// <param name="lpFileName"> /// <returns></returns> public static uint GetIniValue(string lpSection, string lpKeyName, string lpFileName) { // 整数値を読み出す uint resultValue= GetPrivateProfileInt(lpSection, lpKeyName, 0, lpFileName); return resultValue; } //******************************************* // WritePrivateProfileString // //******************************************* /// <summary> /// INIファイルに値を書き込む /// </summary> /// <param name="lpSection">セッション名称 /// <param name="lpKeyName">キー名称 /// <param name="lpValue">セットする値 /// <param name="lpFileName">INIファイル名 /// <returns></returns> public static bool SetIniString(string lpSection, string lpKeyName, string lpValue, string lpFileName) { long result = WritePrivateProfileString(lpSection, lpKeyName, lpValue, lpFileName); return result != 0; } /// <summary> /// /// </summary> /// <param name="lpSection"> /// <param name="lpKeyName"> /// <param name="lpFileName"> public static void DelKeyAndData_pair(string lpSection, string lpKeyName, string lpFileName) { // 1つのキーと値のペアを削除する WritePrivateProfileString(lpSection, lpKeyName, null, lpFileName); } /// <summary> /// /// </summary> /// <param name="lpSection"> /// <param name="lpFileName"> public static void DelKeyAndData_all(string lpSection, string lpFileName) { // 指定セクション内の全てのキーと値のペアを削除する WritePrivateProfileString(lpSection, null, null, lpFileName); } //******************************************* // GetPrivateProfileStringByByteArray // //******************************************* /// <summary> /// キーリスト /// </summary> /// <param name="lpSection"> /// <param name="lpFileName"> /// <returns></returns> public static string[] GetKeys(string lpSection, string lpFileName) { byte[] ar1 = new byte[1024]; uint resultSize1= GetPrivateProfileStringByByteArray(lpSection, null, "", ar1,(uint)ar1.Length, lpFileName); string result1 = Encoding.Default.GetString(ar1, 0, (int)resultSize1 - 1); string[] keys = result1.Split('\0'); return keys; } /// <summary> /// セクションの一覧 /// </summary> /// <param name="lpFileName"> /// <returns></returns> public static string[] GeSections(string lpFileName) { // 指定ファイルのセクションの一覧を得る byte[] ar2 = new byte[1024]; uint resultSize2 = GetPrivateProfileStringByByteArray(null, null, "", ar2, (uint)ar2.Length, lpFileName); string result2 = System.Text.Encoding.Default.GetString(ar2, 0, (int)resultSize2 - 1); string[] sections = result2.Split('\0'); return sections; } } } ------------------------------------------------------------------- using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace INI.IniFiles { class IniProc { public static void Proc() { string FilePath = @"C:\MyFile.ini"; //string s = IniFiles.GetIniString("Section1", "KEY1", FilePath); IniFiles.SetIniString("Section1", "KEY1", "AAAA", FilePath); IniFiles.SetIniString("Section1", "KEY2", "BBBB", FilePath); IniFiles.SetIniString("Section1", "KEY3", "1", FilePath); IniFiles.SetIniString("Section2", "KEY2", "AAAA", FilePath); IniFiles.SetIniString("Section3", "KEY3", "AAAA", FilePath); //----------------------- // 文字列を読み出す //----------------------- StringBuilder sb = new StringBuilder(1024); string sSection = "Section1"; string sKey = "KEY1"; string sRes = IniFiles.GetIniString(sSection, sKey, FilePath); Console.WriteLine("{0}に含まれる{1}の数値: {2}", sSection,sKey, sRes); //----------------------- // 数値を読み出す //----------------------- sSection = "Section1"; sKey = "KEY3"; uint uiRes = IniFiles.GetIniValue(sSection, sKey, FilePath); Console.WriteLine("{0}に含まれる{1}の値: {2}", sSection, sKey, uiRes); //----------------------- // Sectionリスト //----------------------- string[] sections = IniFiles.GeSections(FilePath); foreach (string section in sections) { Console.WriteLine("このファイルに含まれるセクション名: {0}", section); } Console.WriteLine(""); //----------------------- // keyリスト //----------------------- sSection = "Section1"; string[] keys = IniFiles.GetKeys(sSection, FilePath); foreach (string key in keys) { Console.WriteLine( "{0}セクションに含まれるキー名: {1}", sSection,key); } Console.WriteLine(""); } } }
カテゴリー: [C#]
[C#][Tips][File] XML:要素値
public static void mSelectSingleNode_InnerText () { /* File */ var xDoc = new System.Xml.XmlDocument(); xDoc.Load(@"CustomersOrdwes.xml"); /* Node(Single) */ string sXPath= @"[CustomerID='GREAL']/CompanyName"; Console.WriteLine(xDoc.SelectSingleNode(sXPath).InnerText); }
[C#][Tips][File] XML:要素値リスト
public static void mRead_SelectNodes_InnerText() { List<string> slValue = new List<string>(); /* File */ var xDoc = new System.Xml.XmlDocument(); xDoc.Load(@"CustomersOrdwes.xml"); /* Node(List) */ string sXPath = @"/Root/Customers/Customer/CompanyName"; System.Xml.XmlNodeList xNodeList = xDoc.SelectNodes(sXPath); /* Element */ foreach (System.Xml.XmlNode item in xNodeList) { slValue.Add(item.InnerText); } /* */ slValue.ForEach(c => Console.WriteLine(c)); }
[C#][Tips][File] XML:属性値リスト
public static void mRead_SelectNodes_Attrs() { List<string> slValue = new List<string> (); /* File */ var xDoc = new System.Xml.XmlDocument(); xDoc.Load(@"D:\CustomersOrdwes.xml"); /* Node(List) */ string sXPath = @"/Root/Customers/Customer"; System.Xml.XmlNodeList xNodeList = xDoc.SelectNodes(sXPath); /* Element-Attribute */ string sAttrName = "CustomerID"; foreach (System.Xml.XmlNode item in xNodeList) { foreach (System.Xml.XmlNode item_Attr in item.Attributes) { if (item_Attr.Name == sAttrName) slValue.Add((string)item_Attr.Value); } } /* */ slValue.ForEach(c => Console.WriteLine(c)); }
[C#][Tips][File] XML: 指定XPATHの属性値リスト
public static void mRead_SelectSingleNode_Attr() { List<string> slValue = new List<string> (); /* File */ var xDoc = new System.Xml.XmlDocument(); xDoc.Load(@"CustomersOrdwes.xml"); /* Node(Single) */ string sXPath = @"/Root/Customers/Customer[@CustomerID='GREAL']"; var xNode = xDoc.SelectSingleNode(sXPath); /* Element-Attribute */ foreach (System.Xml.XmlNode item_Attr in xNode.Attributes) { slValue.Add((string)item_Attr.Value); } /* */ slValue.ForEach(c => Console.WriteLine(c)); }
[C#][Tips][コントロール] マウスクリック時にテキストボックスを全選択
//========================================= // マウスクリック時にテキストボックスを全選択 //========================================= private void textBox1_MouseClick(object sender, MouseEventArgs e) { ((TextBox)sender).SelectAll(); }
[C#][Tips][コントロール] コントロールの有効/無効
//*********************************************************** /// /// コントロールの有効/無効 /// //*********************************************************** public static void mSetEnabledControl(Control hParent, Boolean bSetStat) { // hParent 内のすべてのコントロールを列挙する foreach (Control cControl in hParent.Controls) { // 列挙したコントロールにコントロールが含まれている場合は再帰呼び出しする if (cControl.HasChildren == true) { mSetEnabledControl(cControl, bSetStat); } cControl.Enabled = bSetStat; } }
[C#][Tips][コントロール] textプロパティクリア(TextBoxBase,ComboBox)
//********************************************************* /// /// textプロパティクリア(TextBoxBase,ComboBox) /// //********************************************************* public static void mClearPropertyText(Control hParent) { // hParent 内のすべてのコントロールを列挙する foreach (Control cControl in hParent.Controls) { // 列挙したコントロールにコントロールが含まれている場合は再帰呼び出しする if (cControl.HasChildren == true) { mClearPropertyText(cControl); } // コントロールの型が TextBoxBase からの派生型の場合は Text をクリアする if (cControl is TextBoxBase) { cControl.Text = string.Empty; } // コントロールの型が ComboBoxの場合は Text をクリアする if (cControl is ComboBox) { cControl.Text = string.Empty; } } }
[C#][Tips][File] INIファイル
namespace libFILE_Ini { //########################### /// [static] //########################### public static class clsProcS { //■□■□■□■□■□■□■□■□■□ //■ DLL //■ //■□■□■□■□■□■□■□■□■□ //****************************************************************** /// //****************************************************************** [DllImport("KERNEL32.DLL")] public static extern uint GetPrivateProfileString( string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, uint nSize, string lpFileName ); //****************************************************************** /// //****************************************************************** [DllImport("KERNEL32.DLL", EntryPoint = "GetPrivateProfileStringA")] public static extern uint GetPrivateProfileStringByByteArray( string lpAppName, string lpKeyName, string lpDefault, byte[] lpReturnedString, uint nSize, string lpFileName ); //****************************************************************** /// //****************************************************************** [DllImport("KERNEL32.DLL")] public static extern uint GetPrivateProfileInt( string lpAppName, string lpKeyName, int nDefault, string lpFileName ); //****************************************************************** /// //****************************************************************** [DllImport("KERNEL32.DLL")] public static extern uint WritePrivateProfileString( string lpAppName, string lpKeyName, string lpString, string lpFileName ); //■□■□■□■□■□■□■□■□■□ //■ //■□■□■□■□■□■□■□■□■□ //****************************************************************** /// //****************************************************************** public class clsINI_GetParam { public string sSection; public string sKey; } //****************************************************************** /// //****************************************************************** public class clsINI_SetParam { public string sSection; public string sKey; public string sData; } //■□■□■□■□■□■□■□■□■□ //■ //■□■□■□■□■□■□■□■□■□ //****************************************************************** /// INIデータ取得 //****************************************************************** public static string mGetIniData(string strIniFile_fp, clsINI_GetParam INI_GetParam) { //================================================== // iniファイルから文字列を取得 //================================================== StringBuilder sb = new StringBuilder(1024); GetPrivateProfileString( INI_GetParam.sSection, // セクション名 INI_GetParam.sKey, // キー名 "", // 値が取得できなかった場合に返される初期値 sb, // 格納先 Convert.ToUInt32(sb.Capacity), // 格納先のキャパ strIniFile_fp); // iniファイル名 //================================================== // RETURN //================================================== return sb.ToString(); } //****************************************************************** /// INIデータ設定 //****************************************************************** public static uint mSetIniData(string strIniFile_fp, clsINI_SetParam INI_SetParam) { //================================================== // ファイル //================================================== return WritePrivateProfileString( INI_SetParam.sSection, // セクション名 INI_SetParam.sKey, // キー名 INI_SetParam.sData, // 書き込む値 strIniFile_fp); // iniファイル名 //if (ret == 0) // MessageBox.Show("失敗"); //else // MessageBox.Show("成功"); } }
[C#][Tips][LINQ] 集計(最大,最小,平均,合計,要素数,アキュムレータ)
//********************************************** /// /// [LIST] 集計(最大,最小,平均,合計,要素数,アキュムレータ) /// //********************************************** public static void mAggregation() { var ilList = new<int> List() { 11, 2, 23, 44, 29 }; /* 最大 */ Console.WriteLine(ilList.Max()); /* 最小 */ Console.WriteLine(ilList.Min()); /* 平均 */ Console.WriteLine(ilList.Average()); /* 合計 */ Console.WriteLine(ilList.Sum()); /* 要素数 */ Console.WriteLine(ilList.Count()); /* アキュムレータ(最大) */ /* アキュムレータ(累積器)関数で処理した結果(関数:Math.Max(now, next)) */ // now:処理結果 // next:配列を順に設定 Console.WriteLine(ilList.Aggregate((now, next) => Math.Max(now, next))); /* 参考URL */ //http://ameblo.jp/ns-programming-memo/entry-11064901160.html // http://d.hatena.ne.jp/gsf_zero1/20101005/p1 // http://chomado.com/programming/c-sharp/c-aggregate%E3%82%92%E4%BD%BF%E3%81%86linq ilList = new <int> List() { 2, 3, 4, 5, 6 }; Console.WriteLine(ilList.Aggregate((sum, i) => sum + i)); // sum , i => sum , i // (init) 0 , 2 => 0 + 2 = 2 (=sum) //2 , 3 => 2 + 3 = 5 (=sum) //5 , 4 => 5 + 4 = 9 (=sum) //9 , 5 => 9 + 5 = 14 (=sum) // 14 , 6 => 14 + 6 = 20 (=sum) ilList = new <int>List() { 2, 3, 4, 5, 6 }; Console.WriteLine(ilList.Aggregate(10,(sum, i) => sum + i)); // sum , i => sum , i // (init)10 , 2 => 10 + 2 = 12 (=sum) // 12 , 3 => 12 + 3 = 15 (=sum) // 15 , 4 => 15 + 4 = 19 (=sum) // 19 , 5 => 19 + 5 = 24 (=sum) // 24 , 6 => 24 + 6 = 30 (=sum) }