LoginSignup
0
0

More than 1 year has passed since last update.

cscの作法 その237

Last updated at Posted at 2022-07-13

概要

cscの作法、調べてみた。
FileSystemWatcher、使ってみた。

参考にしたページ

写真

image.png

サンプルコード

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading;
using System.Windows.Forms;

namespace RImageConverter
{
	public partial class Form1 : Form {
		private delegate void SafeCallDelegate(string text);
		private FileSystemWatcher watcher = new FileSystemWatcher();
		private string moveFolder;
		private Button button1;
		private TextBox textBox1;
		private TextBox textBox2;
		private TextBox textBox3;
		private System.Windows.Forms.NotifyIcon notifyIcon1;
		public Form1() {
			this.button1 = new Button();

			this.textBox1 = new TextBox();
			this.textBox2 = new TextBox();
			this.textBox3 = new TextBox();

			this.button1.Location = new System.Drawing.Point(300, 130);
			this.button1.Name = "button1";
			this.button1.TabIndex = 0;
			this.button1.Text = "start";
			this.button1.Click += new System.EventHandler(this.button1_Click);

			this.textBox1.Location = new System.Drawing.Point(30, 20);
			this.textBox1.Multiline = true;
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(300, 100);
			this.textBox1.TabIndex = 2;
			this.textBox1.Text = @"C:\Users\user\Desktop\iroiro";

			this.textBox2.Location = new System.Drawing.Point(30, 160);
			this.textBox2.Multiline = true;
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(300, 100);
			this.textBox2.TabIndex = 2;
			this.textBox2.Text = @"C:\Users\user\Desktop";

			this.textBox3.Location = new System.Drawing.Point(30, 300);
			this.textBox3.Multiline = true;
			this.textBox3.Name = "textBox3";
			this.textBox3.Size = new System.Drawing.Size(300, 100);
			this.textBox3.TabIndex = 2;
			this.textBox3.Text = "";

			this.ClientSize = new System.Drawing.Size(400, 500);
			this.Controls.AddRange(new Control[] {
				this.button1,
				this.textBox1,
				this.textBox2,
				this.textBox3
			});

			this.notifyIcon1 = new System.Windows.Forms.NotifyIcon();
			this.notifyIcon1.Text = "ic0";
			this.notifyIcon1.Visible = true;
			this.notifyIcon1.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.notifyIcon1_MouseDoubleClick);

			watcher.Filter = "*.bmp";
			watcher.Created += new FileSystemEventHandler(watcher_Created);
		}
		private void AddLog(string text) {
			if (textBox1.InvokeRequired)
			{
				var d = new SafeCallDelegate(AddLog);
				textBox1.Invoke(d, new object[] { 
					text 
				});
			}
			else
			{
				textBox3.Text = String.Format("{0}\r\n{1}", text, textBox3.Text);
			}
		}
		private void watcher_Created(object sender, FileSystemEventArgs args) {
			try
			{
				//Thread.Sleep(1000);
				using (var bmp = new Bitmap(args.FullPath))
				{
					var dateFolder = DateTime.Now.ToString("yyyy-MM-dd");
					Directory.CreateDirectory(String.Format(@"{0}\{1}", textBox2.Text, dateFolder));
					var path = String.Format(@"{0}\{1}\{2}", moveFolder, dateFolder, args.Name.Replace(".bmp", ".png"));
					bmp.Save(path, ImageFormat.Png);
					AddLog(path);
				}
				File.Delete(args.FullPath);
			}
			catch (Exception ex)
			{
				AddLog(ex.Message);
			}
		}
		private void button1_Click(object sender, EventArgs e) {
			if (button1.Text.Equals("監視開始"))
			{
				watcher.Path = textBox1.Text;
				moveFolder = textBox2.Text;
				watcher.EnableRaisingEvents = true;
				button1.Text = "監視停止";
			}
			else
			{
				watcher.EnableRaisingEvents = false;
				button1.Text = "監視開始";
			}
		}
		private void Form1_Resize(object sender, EventArgs e) {
			if (this.WindowState == FormWindowState.Minimized)
			{
				this.Visible = false;
				notifyIcon1.Visible = true;
			}
		}
		private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) {
			this.Visible = true;
			this.WindowState = FormWindowState.Normal;
			notifyIcon1.Visible = false;
		}
		[STAThread]
		static void Main() {
			Application.Run(new Form1());
		}
	}
}





以上。

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