LoginSignup
0
0

More than 1 year has passed since last update.

snippet: C#

Last updated at Posted at 2021-06-12

コンパイル方法

2重実行を防ぐ

main()関数内に以下記述

//ミューテックス作成
_mutex = new System.Threading.Mutex(false, "MYSOFTWARE_001");
     
//ミューテックスの所有権を要求する
if (_mutex.WaitOne(0, false) == false) {
  MessageBox.Show("本ソフトウェアは複数起動できません。");
  return;
}

キー入力

main.cs
using System;

namespace ConsoleApp1 {
	class Program {
		static void Main(string[] args) {

			// Altキー押しながらf
			SendKeys.SendWait("(%f)");
			System.Threading.Thread.Sleep(100);
			// aキー
			SendKeys.SendWait("a");
			System.Threading.Thread.Sleep(100);

		}
	}
}

文字

コンソール出力(途中改行付き)

main.cs
using System;

namespace ConsoleApp1 {
	class Program {
		static void p(string str) { Console.WriteLine(str); }
		static void Main(string[] args) {

			// 改行は \n
			string str = "あいうえおabcdeかきくけこ\nさしすせそタチツテト";
			p(str);

		}
	}
}

改行付き文字を変数に入れる

main.cs
using System;

namespace ConsoleApp1 {
	class Program {
		static void p(string str) { Console.WriteLine(str); }
		static void Main(string[] args) {

			string str = @"いろは
にほへと
";
			p(str);

		}
	}
}

文字を繰り返す

  • 10個あを表示
main.cs
using System;

namespace ConsoleApp1 {
	class Program {
		static void p(string str) { Console.WriteLine(str); }
		static void Main(string[] args) {

			string text = "";
			for (int i=0; i<10; i++) {
				text += "あ";
			}
			p(text);

		}
	}
}
main.cs
using System;

namespace ConsoleApp1 {
	class Program {
		static void p(string str) { Console.WriteLine(str); }
		static void Main(string[] args) {

			string text = new String('あ', 10);
			p(text);

		}
	}
}

文字列の置換(正規表現)

main.cs
using System;
using System.Text.RegularExpressions;

namespace ConsoleApp1 {
	class Program {
		static void p(string str) { Console.WriteLine(str); }
		static void Main(string[] args) {

			string inputText = @"<div>aaa</div><div>bbb</div>";
			// HTMLタグを除去する例
			string pattern = @"<.*?>";
			// 空白だけの行を除去。1行ごとに処理するので、RegexOptions.Multilineを指定
			string ResultText = Regex.Replace(inputText, pattern, "", RegexOptions.Multiline);
			// aaabbb
			p(ResultText);

		}
	}
}

コマンドラインから入力した文字を扱う

main.cs
using System;

namespace ConsoleApp1 {
	class Program {
		static void p(string str) { Console.WriteLine(str); }
		static void Main(string[] args) {

			p(@"なにか入力してください: ");
			string InputText = Console.ReadLine();
			p(InputText);

		}
	}
}

メモ: Console.Inを使う場合は、Control-Dで入力を終える。


音を出す(Beep)

main.cs
using System;
using System.Runtime.InteropServices;

class BeepProgram {
	[DllImport("kernel32.dll")]
	private extern static bool Beep(uint dwFreq, uint dwDuration);

	private static void Main() {
		Beep(262, 500);  // ド
		Beep(294, 500);  // レ
		Beep(330, 500);  // ミ
		Beep(349, 500);  // ファ
		Beep(392, 500);  // ソ
		Beep(440, 500);  // ラ
		Beep(494, 500);  // シ
		Beep(523, 500);  // ド
	}
}

コマンドライン

コマンドライン引数

main.exe 1 2 3

main.cs
using System;

namespace ConsoleApp1 {
    class Program {
        static void p(string str) { Console.WriteLine(str); }
        static void Main(string[] args) {

		p(Environment.CommandLine);
		// forループで分割
		string[] str = Environment.GetCommandLineArgs();
		foreach(string s in str) {
			p(s);
		}
		p(str[1]);
		p(str[2]);

        }
    }
}

sleep

main.cs
using System;

namespace ConsoleApp1 {
    class Program {
        static void p(string str) { Console.WriteLine(str); }
        static void Main(string[] args) {

		p("sleep start");
		// 3秒スリープ
		System.Threading.Thread.Sleep(3000);
		p("sleep end");


        }
    }
}

テキストファイルの読み込み

改行区切り

main.cs
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApp1 {
	class Program {
		static void p(string str) { Console.WriteLine(str); }
		static void Main(string[] args) {

			// 読み込み
			string source = File.ReadAllText("main.cs", Encoding.GetEncoding("shift_jis"));
			// 行末のスペースを無視しつつ、改行区切り
			string pattern = @"\s*\r\n";
			string[] lines = Regex.Split(source, pattern);
			// 正規表現で分割
			foreach(var line in lines) {
				p(line);
			}

		}
	}
}

alert

MessageBox.Show("こんにちは");
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0