Public static viod mList_Join() { List<int> il1 = new List<int> { 1, 2, 3 }; List<int> il2 = new List<int> { 4, 5, 6 }; List<int> il = il1.Concat(il2).ToList(); il.ForEach(c => Console.Write(c + ",")); }
カテゴリー: [C#]
[C#][Tips] コンパネ制御
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ControlPanel { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //####################################################### //# SAMPLE //####################################################### #region /* //「インターネットのプロパティ」を開く System.Diagnostics.Process.Start("control.exe", "inetcpl.cpl"); //関連付けで開くには、次のようにする //System.Diagnostics.Process.Start("inetcpl.cpl"); //2枚目以降のタブを開くには、次のようにインデックスを指定する //「インターネットのプロパティ」の「プライバシー」ページを開く System.Diagnostics.Process.Start("control.exe", "inetcpl.cpl,,2"); //「キーボードのプロパティ」を開く System.Diagnostics.Process.Start("control.exe", "main.cpl @1"); */ #endregion //******************************************************* /// <summary> /// 「ディスクの管理」diskmgmt.msc /// </summary> /// <param name="sender"></param> /// <param name="e"></param> //******************************************************* private void btn_ディスクの管理_Click(object sender, EventArgs e) { //mmc.exe diskmgmt.msc System.Diagnostics.Process.Start("mmc.exe", "diskmgmt.msc"); } //******************************************************* /// <summary> /// 「システムのプロパティ」sysdm.cpl /// sysdm.cpl /// </summary> /// <param name="sender"></param> /// <param name="e"></param> //******************************************************* private void btn_システムのプロパティ_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start("control.exe", "sysdm.cpl"); } //******************************************************* /// <summary> /// 「コンピューターの管理」compmgmt.msc /// </summary> /// <param name="sender"></param> /// <param name="e"></param> //******************************************************* private void btn_コンピュータの管理_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start("mmc.exe", "compmgmt.msc"); // コンピュータの管理 } //******************************************************* /// <summary> /// 「ユーザーとグループ」 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> //******************************************************* private void btn_ローカルユーザーとグループ_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start("mmc.exe", "lusrmgr.msc"); // ローカルユーザーとグループ } //******************************************************* /// <summary> /// 「タスクバーとスタートメニュー」 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> //******************************************************* private void btn_タスクバーとスタートメニュー_Click(object sender, EventArgs e) { //■タスクバー System.Diagnostics.Process.Start("control.exe", "/name Microsoft.TaskbarAndStartmenu"); /* //■[スタート]メニュー(Win10なし) System.Diagnostics.Process.Start("control.exe", "/name Microsoft.TaskbarAndStartmenu /page pageStartmenu"); System.Diagnostics.Process.Start("rundll32.exe", "shell32.dll, Options_RunDLL 3"); //■ツールバー System.Diagnostics.Process.Start("rundll32.exe", "shell32.dll, Options_RunDLL 6"); */ } //******************************************************* /// <summary> /// 「ユーザーアカウント制御の設定」 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> //******************************************************* private void btn_ユーザーアカウント制御の設定_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start("UserAccountControlSettings.exe"); } //******************************************************* /// <summary> /// 電源オプション /// </summary> /// <param name="sender"></param> /// <param name="e"></param> //******************************************************* private void btn_電源オプション_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start("control.exe", "powercfg.cpl"); } //******************************************************* /// <summary> /// 「デスクトップアイコンの設定」 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> //******************************************************* private void btn_デスクトップアイコンの設定_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start("rundll32.exe", "shell32.dll,Control_RunDLL desk.cpl,,0"); } //******************************************************* /// <summary> /// 「スクリーンセーバーの設定」 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> //******************************************************* private void btn_スクリーンセーバーの設定_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start("control.exe", "desk.cpl,,1"); } //******************************************************* /// <summary> /// 「フォルダーオプション」 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> //******************************************************* private void btn_フォルダーオプション_Click(object sender, EventArgs e) { //control /name Microsoft.FolderOptions //■フォルダオプション //System.Diagnostics.Process.Start("control.exe", "/name Microsoft.FolderOptions"); //■フォルダオプション:表示 System.Diagnostics.Process.Start("rundll32.exe", "shell32.dll,Options_RunDLL 7"); } private void btn_スタートメニュー_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start("EXPLORER.EXE", @"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\"); } private void btn_システムの保護_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start("SystemPropertiesProtection.exe"); } private void btn_リモート_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start("SystemPropertiesRemote.exe"); } } }
C#:Tips:LINQでの条件設定(Where拡張メソッド)
LINQで条件設定:Where拡張メソッドを使用
public static void m_Where拡張メソッド() { // 1::10 IEnumerable<int> ilList = Enumerable.Range(1, 10); Console.Write("List: "); var ilBuf = ilList.ToList(); for (int i = 0; i < ilBuf.Count(); i++) { Console.Write("[{0}]{1} ", i, ilBuf[i]); } Console.WriteLine(); //============================================ // 偶数リスト(各要素で2の余剰が0のものを抽出) //============================================ ilResult = ilList.Where(n => n % 2 == 0); Console.Write("偶数値: "); ilResult.ToList().ForEach(n => Console.Write("{0} ", n)); Console.WriteLine(); //============================================ // 偶数リストを合計 //============================================ var iResult = ilList.Where(n => n % 2 == 0).Sum(); Console.Write("偶数の合計:{0} ", iResult); Console.WriteLine(); //============================================ // Nullを除いた、最大・最小 //============================================ int?[] iaNumbers = { 1, 2, null, 3 }; var vMin = iaNumbers.Where(n => n != null).Min(); Console.WriteLine("Min(nullを除く) = {0}", vMin); var vMax = iaNumbers.Where(n => n != null).Max(); Console.WriteLine("Max(nullを除く) = {0}", vMax); Console.WriteLine(); //============================================ // 偶数インデックス // (コレクションの条件指定(別途説明)) //============================================ // where拡張メソッドのオーバーロード // ARG1:対象値 // ARG2:インデックス //------------------------------------------ var ilResult = ilList.Where((w, n) => n % 2 == 0); Console.Write("偶数カラム: "); ilResult.ToList().ForEach(n => Console.Write("{0} ", n)); Console.WriteLine(); }
C#:Tips:合計・最大・最小・平均(LINQ)
合計・最大・最小・平均(LINQ)
/// <summary> /// /// </summary> class clsSum { public static void m_Sum() { IEnumerable<int> ilList = Enumerable.Range(1, 10); int iSum = ilList.Sum(); Console.WriteLine("Sum = {0}",iSum); int iMin = ilList.Min(); Console.WriteLine("Min = {0}", iMin); int iMax = ilList.Max(); Console.WriteLine("Max = {0}", iMax); double dAve = ilList.Average(); Console.WriteLine("Ave = {0}", dAve); //============================================ // Nullを除いた、最大・最小 //============================================ int?[] iaNumbers = { 1, 2, null, 3 }; var vMin = iaNumbers.Where(n => n != null).Min(); Console.WriteLine("Min(nullを除く) = {0}", vMin); var vMax = iaNumbers.Where(n => n != null).Max(); Console.WriteLine("Max(nullを除く) = {0}", vMax); } }
C#:Tips:xからyまでの整数が入ったコレクションを作成する
・1から10までのコレクション作成
・foreachで要素表示
・LINQで要素表示
コレクション作成
public static void main() { IEnumerable<int> ilList = Enumerable.Range(1, 10); clsWriteNumbers.m_WriteNumbers(ilList, "コレクション"); }
表示
/// <summary> /// /// </summary> class clsWriteNumbers { /// <summary> /// /// </summary> /// <param name="iNumbers"></param> /// <param name="sHeader"></param> public static void m_WriteNumbers(IEnumerable<int> iNumbers, string sHeader) { // foreach Console.Write("{0}: ", sHeader); foreach (var item in iNumbers) { Console.Write(item); } Console.WriteLine(); // LINQ Console.Write("{0}: ", sHeader); iNumbers.ToList().ForEach(n => Console.Write(n)); // for VS2015(C#6) //System.Console.Write($"{sHeader}"); } }
C#:基礎:配列
namespace c06_配列とコレクション.s019_配列 { //■■■■■■■■■■■■■■■■■■■■■■ /// <summary> /// n01_配列の基礎 : /// </summary> //■■■■■■■■■■■■■■■■■■■■■■ public partial class n01_配列の基礎 { public static void m_配列の基礎() { // 宣言 string[] saArray = new string[5]; // 代入 saArray[0] = "array1"; saArray[1] = "array2"; saArray[2] = "array3"; saArray[3] = "array4"; saArray[4] = "array5"; // 読み出し(for) Console.WriteLine("-- [for] -----"); for (int i = 0; i < saArray.Length; i++) { Console.WriteLine(saArray[i]); } // 読み出し(foreach) Console.WriteLine("-- [foreach] -----"); foreach (var item in saArray) { Console.WriteLine(item); } } } }
[C#][Tips] サービス制御
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.ServiceProcess; namespace サービス制御 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { string sSrevice = "World Wide Web Publishing Service"; this.txtStatus.Text = String.Empty; //usingステートメントは範囲から抜けた際に自動的にDisposeなどしてくれるので便利です。 //「任意のWindowsサービス」は動作したいサービス名を指定してください。 using (ServiceController sc = new ServiceController(sSrevice)) { //プロパティ値を更新 sc.Refresh(); //起動中 if (sc.Status == ServiceControllerStatus.Running) { // 停止 sc.Stop(); this.txtStatus.Text = "Stop"; // 一時停止 //sc.Pause(); } //停止中 else if (sc.Status == ServiceControllerStatus.Stopped) { // 開始 sc.Start(); this.txtStatus.Text = "Run"; } //一時停止中 else if (sc.Status == ServiceControllerStatus.Paused) { // 再開 sc.Continue(); this.txtStatus.Text = "Run"; // 停止 //sc.Stop(); } } } private void textBox1_TextChanged(object sender, EventArgs e) { } } }
C#:画像判定
C# ソース表示
string s = "Hello";