cscの作法 その165

# 概要
cscの作法、調べてみた。
Configuration使ってみた。

# 参考にしたページ

https://qiita.com/yourao424/items/d2f2041f681548c264f4

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/1a739fc4-ad8e-07b0-5b85-d13d1719a2f4.png)

# サンプルコード

```
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Diagnostics;

namespace app
{
	public class Folder {
		public Folder(int id, int num, string name) {
			this.Id = id;
			this.Num = num;
			this.Name = name;
		}
		public int Id {
			get {
				return Id;
			}
			set {
				Id = value;
			}
		}
		public int Num {
			get {
				return Num;
			}
			set {
				Num = value;
			}
		}
		public string Name {
			get {
				return Name;
			}
			set {
				Name = value;
			}
		}
		public string GetFolderName() {
			return string.Format("{0}_{1}", this.Num.ToString("00"), this.Name);
		}
		public void Create(string baseDirectory) {
			if (string.IsNullOrEmpty(this.Name))
			{
				return;
			}
			string path = Path.Combine(baseDirectory, this.GetFolderName());
			DirectoryInfo directoryInfo = new DirectoryInfo(path);
			if (!directoryInfo.Exists)
			{
				directoryInfo.Create();
			}
		}
	}
	public partial class Form1 : Form {
		private System.Windows.Forms.TextBox baseDirectory;
		private System.Windows.Forms.Button baseDirectrySelectBtn;
		private System.Windows.Forms.Label baseDirectoryLabel;
		private System.Windows.Forms.Button addButton;
		private System.Windows.Forms.GroupBox folderGroup;
		private System.Windows.Forms.Button createButton;
		private System.Windows.Forms.Button removeButton;
		Dictionary<string, string> configs = new Dictionary<string, string>();
		List<Folder> _folderList = new List<Folder>();
		public Form1() {
			InitializeComponent();
			baseDirectory.Text = LoadSetting("Base");
			int length = int.Parse(LoadSetting("Display"));
			for (int i = 0; i < length; i++)
			{
				CreatePanel(true);
			}
		}
		private void InitializeComponent() {
			this.baseDirectory = new System.Windows.Forms.TextBox();
			this.baseDirectrySelectBtn = new System.Windows.Forms.Button();
			this.baseDirectoryLabel = new System.Windows.Forms.Label();
			this.addButton = new System.Windows.Forms.Button();
			this.folderGroup = new System.Windows.Forms.GroupBox();
			this.removeButton = new System.Windows.Forms.Button();
			this.createButton = new System.Windows.Forms.Button();
			this.SuspendLayout();
			this.baseDirectory.Location = new System.Drawing.Point(13, 31);
			this.baseDirectory.Name = "baseDirectory";
			this.baseDirectory.Size = new System.Drawing.Size(349, 23);
			this.baseDirectory.TabIndex = 0;
			this.baseDirectrySelectBtn.Location = new System.Drawing.Point(368, 31);
			this.baseDirectrySelectBtn.Name = "baseDirectrySelectBtn";
			this.baseDirectrySelectBtn.Size = new System.Drawing.Size(33, 23);
			this.baseDirectrySelectBtn.TabIndex = 1;
			this.baseDirectrySelectBtn.Text = "...";
			this.baseDirectrySelectBtn.UseVisualStyleBackColor = true;
			this.baseDirectrySelectBtn.Click += new System.EventHandler(this.BaseDirectrySelectBtn_Click);
			this.baseDirectoryLabel.AutoSize = true;
			this.baseDirectoryLabel.Location = new System.Drawing.Point(13, 13);
			this.baseDirectoryLabel.Name = "baseDirectoryLabel";
			this.baseDirectoryLabel.Size = new System.Drawing.Size(75, 15);
			this.baseDirectoryLabel.TabIndex = 2;
			this.baseDirectoryLabel.Text = "フォルダを選択";
			this.addButton.Location = new System.Drawing.Point(13, 61);
			this.addButton.Name = "addButton";
			this.addButton.Size = new System.Drawing.Size(57, 23);
			this.addButton.TabIndex = 13;
			this.addButton.Text = "+";
			this.addButton.UseVisualStyleBackColor = true;
			this.addButton.Click += new System.EventHandler(this.AddButton_Click);
			this.folderGroup.Location = new System.Drawing.Point(13, 90);
			this.folderGroup.Name = "folderGroup";
			this.folderGroup.Size = new System.Drawing.Size(388, 365);
			this.folderGroup.TabIndex = 14;
			this.folderGroup.TabStop = false;
			this.removeButton.Location = new System.Drawing.Point(76, 61);
			this.removeButton.Name = "removeButton";
			this.removeButton.Size = new System.Drawing.Size(57, 23);
			this.removeButton.TabIndex = 14;
			this.removeButton.Text = "-";
			this.removeButton.UseVisualStyleBackColor = true;
			this.removeButton.Click += new System.EventHandler(this.RemoveButton_Click);
			this.createButton.Location = new System.Drawing.Point(326, 461);
			this.createButton.Name = "createButton";
			this.createButton.Size = new System.Drawing.Size(75, 23);
			this.createButton.TabIndex = 15;
			this.createButton.Text = "作成";
			this.createButton.UseVisualStyleBackColor = true;
			this.createButton.Click += new System.EventHandler(this.CreateButton_Click);
			this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(413, 488);
			this.Controls.Add(this.removeButton);
			this.Controls.Add(this.createButton);
			this.Controls.Add(this.addButton);
			this.Controls.Add(this.folderGroup);
			this.Controls.Add(this.baseDirectoryLabel);
			this.Controls.Add(this.baseDirectrySelectBtn);
			this.Controls.Add(this.baseDirectory);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.MaximizeBox = false;
			this.Name = "Form1";
			this.Text = "FolderCreation";
			this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
			this.ResumeLayout(false);
			this.PerformLayout();
		}
		private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
			SaveSetting();
		}
		private void BaseDirectrySelectBtn_Click(object sender, EventArgs e) {
			FolderBrowserDialog fbd = new FolderBrowserDialog();
			fbd.RootFolder = Environment.SpecialFolder.Desktop;
			DialogResult result =  fbd.ShowDialog(this);
			if (result == DialogResult.OK)
			{
				string path = fbd.SelectedPath;
				this.baseDirectory.Text = path;
			}
		}
		private void CreateButton_Click(object sender, EventArgs e) {
			if (string.IsNullOrEmpty(this.baseDirectory.Text))
			{
				MessageBox.Show("ディレクトリを指定してください", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			_folderList.Clear();
			int i = 1;
			Control.ControlCollection controls = this.folderGroup.Controls;
			foreach (Control control in controls)
			{
				if (control.GetType() != typeof(Panel))
				{
					continue;
				}
				Control.ControlCollection panel = control.Controls;
				NumericUpDown numeric = (NumericUpDown) panel[0];
				TextBox name = (TextBox) panel[1];
				Folder folder = new Folder(i, (int) numeric.Value, name.Text);
				folder.Create(baseDirectory.Text);
				_folderList.Add(folder);
				i++;
			}
		}
		private void AddButton_Click(object sender, EventArgs e) {
			int controlCount = this.folderGroup.Controls.Count;
			if (controlCount >= int.Parse(LoadSetting("MaxDisplay")))
			{
				return;
			}
			CreatePanel(false);
		}
		private void RemoveButton_Click(object sender, EventArgs e) {
			int controlCount = this.folderGroup.Controls.Count;
			if (controlCount <= 1)
			{
				return;
			}
			RemovePanel();
		}
		private string LoadSetting(string key) {
			ConfigurationManager.OpenExeConfiguration(@Process.GetCurrentProcess().MainModule.FileName);
			foreach (string key1 in ConfigurationManager.AppSettings.AllKeys)
			{
				configs[key1] = ConfigurationManager.AppSettings[key1];
				//Console.WriteLine(key1);
				//Console.WriteLine(ConfigurationManager.AppSettings[key1]);
			}
			string a;
			if (!configs.TryGetValue(key, out a))
			{
				Console.WriteLine(key);
				return null;
			}
			return configs[key];
		}
		private void SaveSetting() {
			if (_folderList.Count == 0)
			{
				//return;
			}
			Configuration config = ConfigurationManager.OpenExeConfiguration(@Process.GetCurrentProcess().MainModule.FileName);
			config.AppSettings.Settings["Base"].Value = baseDirectory.Text;
			int length = int.Parse(LoadSetting("MaxDisplay"));
			for (int i = 1; i <= length; i++)
			{
				config.AppSettings.Settings[string.Format("Number{0}", i)].Value = i.ToString();
				config.AppSettings.Settings[string.Format("Value{0}", i)].Value = "";
			}
			foreach (Folder folder in _folderList)
			{
				config.AppSettings.Settings[string.Format("Number{0}", folder.Id)].Value = folder.Num.ToString();
				config.AppSettings.Settings[string.Format("Value{0}", folder.Id)].Value = folder.Name;
			}
			config.AppSettings.Settings["Display"].Value = _folderList.Count.ToString();
			config.Save();
		}
		private void CreatePanel(bool isInit) {
			Control.ControlCollection collection = this.folderGroup.Controls;
			int y = collection.Count != 0 ? collection[collection.Count - 1].Location.Y + 35 : 10;
			int id = collection.Count != 0 ? collection.Count + 1 : 1;
			int num = isInit ? int.Parse(LoadSetting(string.Format("Number{0}", id))) : id;
			string value = isInit ? LoadSetting(string.Format("Value{0}", id)) : "";
			Panel panel = new Panel();
			NumericUpDown directoryNumber = new NumericUpDown();
			directoryNumber.Name = string.Format("directoryNumber{0}", id);
			directoryNumber.Value = num;
			directoryNumber.Size = new Size(35, 20);
			directoryNumber.Location = new Point(10, 5);
			directoryNumber.Minimum = 1;
			directoryNumber.Maximum = 99;
			TextBox directory = new TextBox();
			directory.Name = "directory{id}";
			directory.Text = value;
			directory.Size = new Size(270, 25);
			directory.Location = new Point(50, 5);
			directory.TextChanged += CheckFileName;
			panel.Controls.Add(directoryNumber);
			panel.Controls.Add(directory);
			panel.Location = new Point(5, y);
			panel.Size = new Size(380, 30);
			this.folderGroup.Controls.Add(panel);
		}
		private void RemovePanel() {
			this.folderGroup.Controls[this.folderGroup.Controls.Count - 1].Dispose();
		}
		private void CheckFileName(object sender, EventArgs e) {
			TextBox textbox = (TextBox) sender;
			string name = textbox.Text;
			ToolTip toolTip = new ToolTip();
			toolTip.ShowAlways = true;
			char[] invalidChars = Path.GetInvalidFileNameChars();
			if (name.IndexOfAny(invalidChars) > -1 || Regex.IsMatch(name, @"(^|\\|/)(CON|PRN|AUX|NUL|CLOCK\$|COM[0-9]|LPT[0-9])(\.|\\|/|$)", RegexOptions.IgnoreCase))
			{
				textbox.BackColor = Color.PaleVioletRed;
				toolTip.SetToolTip(textbox, "使用出来ない文字が含まれています。");
				this.createButton.Enabled = false;
				return;
			}
			textbox.BackColor = Color.White;
			toolTip.SetToolTip(textbox, null);
			this.createButton.Enabled = true;
			return;
		}
		[STAThread]
		static void Main() {
			Application.Run(new Form1());
		}
	}
}





```

以上。