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.Data.OleDb;
using System.IO;
namespace ファイル認証フォーム
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
this.Text = "ファイル認証フォーム 1.0";
create_dgv();
if (!load_dgv()) return;
dataGridView1.CellDoubleClick += new DataGridViewCellEventHandler(onclick_dgv);
}
private void click_approval(string id, string approval_name)
{
string qry;
string msg;
if (approval_name != "")
{
DialogResult result = MessageBox.Show("既に認証済みです。認証を解除しますか?", "", MessageBoxButtons.YesNo);
if (result == DialogResult.No) return;
UserPassword f = new UserPassword(approval_name);
f.ShowDialog();
if (!f.approval_check) return;
qry = "update dat_tbl set approval_name = null where id = " + id + ";";
msg = "解除";
}
else
{
UserPassword f = new UserPassword();
f.ShowDialog();
if (!f.approval_check) return;
qry = "update dat_tbl set approval_name = '" + f.approval_user + "' where id = " + id + ";";
msg = "登録";
}
using (OleDbConnection con = new OleDbConnection(UserStrings.ds_dat))
{
try
{
con.Open();
}
catch
{
MessageBox.Show("データーベース接続にエラーが発生しました。");
return;
}
OleDbCommand cmd = new OleDbCommand(qry, con);
try
{
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("正しく" + msg + "できませんでした。");
return;
}
MessageBox.Show(msg + "しました。");
load_dgv();
}
}
private void click_delete(string id, string approval_name, string file_name)
{
DialogResult result = MessageBox.Show(file_name + "\n" + "削除しますか?", "", MessageBoxButtons.YesNo);
if (result == DialogResult.No) return;
if (approval_name == "")
{
row_delete(id);
}
else
{
UserPassword f = new UserPassword(approval_name);
f.ShowDialog();
if (!f.approval_check)
{
MessageBox.Show("承認者以外は削除できません。");
return;
}
row_delete(id);
}
}
private void click_open(string id, string approval_name, string file_name, string file_path)
{
if (!File.Exists(file_path))
{
MessageBox.Show("ファイルが存在しません。");
return;
}
string current_hash;
using (FileStream fs = new FileStream(file_path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] bs = md5.ComputeHash(fs);
md5.Clear();
StringBuilder res = new StringBuilder();
foreach (byte b in bs)
{
res.Append(b.ToString("x2"));
}
current_hash = res.ToString();
}
using (OleDbConnection con = new OleDbConnection(UserStrings.ds_dat))
{
try
{
con.Open();
}
catch
{
MessageBox.Show("データーベース接続にエラーが発生しました。");
return;
}
try
{
string q = "select * from dat_tbl where id = " + id + ";";
OleDbCommand cmd = new OleDbCommand(q, con);
using (OleDbDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
if (dr["md5"].ToString() == current_hash)
{
System.Diagnostics.Process.Start(Path.GetDirectoryName(file_path));
return;
}
}
MessageBox.Show("ハッシュ値が一致しません。");
return;
}
}
catch
{
MessageBox.Show("データ取得中にエラーが発生しました。");
}
}
}
private void row_delete(string id)
{
using (OleDbConnection con = new OleDbConnection(UserStrings.ds_dat))
{
try
{
con.Open();
}
catch
{
MessageBox.Show("データーベース接続にエラーが発生しました。");
return;
}
string q = "delete from dat_tbl where id = " + id + ";";
OleDbCommand cmd = new OleDbCommand(q, con);
try
{
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("正しく削除できませんでした。");
return;
}
MessageBox.Show("削除しました。");
load_dgv();
}
}
private void onclick_dgv(object sender, DataGridViewCellEventArgs e)
{
int c = e.ColumnIndex;
int r = e.RowIndex;
string id = dataGridView1.Rows[r].Cells[0].Value.ToString();
string approval_name = dataGridView1.Rows[r].Cells[1].Value.ToString();
string file_name = dataGridView1.Rows[r].Cells[4].Value.ToString();
string file_path = dataGridView1.Rows[r].Cells[5].Value.ToString();
if (c == 1 && r >= 0) click_approval(id, approval_name);
if (c == 4 && r >= 0) click_open(id, approval_name, file_name, file_path);
if (c == 5 && r >= 0) click_delete(id, approval_name, file_name);
}
private void create_dgv()
{
string[] headers = new string[] {
"", //0
"承認者 (承認)",
"得意先",
"種類",
"ファイル名 (ファイルオープン)",//4
"パス名 (登録削除)",//5
"ハッシュ値",
"更新日時"
};
foreach (string h in headers)
{
DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
col.HeaderText = h;
dataGridView1.Columns.Add(col);
}
dataGridView1.ReadOnly = true;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView1.Columns[0].Visible = false;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
dataGridView1.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False;
dataGridView1.Font = new Font("MS UI Gothic", 12);
dataGridView1.RowTemplate.Height = 32;
dataGridView1.AllowUserToAddRows = false;
}
public bool load_dgv(string q = "select * from dat_tbl order by updated;")
{
dataGridView1.Rows.Clear();
using (OleDbConnection con = new OleDbConnection(UserStrings.ds_dat))
{
try
{
con.Open();
}
catch
{
MessageBox.Show("データーベース接続にエラーが発生しました。");
return false;
}
try
{
OleDbCommand cmd = new OleDbCommand(q, con);
using (OleDbDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
dataGridView1.Rows.Add(
dr["id"].ToString(),
dr["approval_name"].ToString(),//1
dr["customer"].ToString(),
dr["document_type"].ToString(),
dr["file_name"].ToString(),//4
dr["file_path"].ToString(),//5
dr["md5"].ToString(),
dr["updated"].ToString()
);
}
}
}
catch
{
MessageBox.Show("データ取得中にエラーが発生しました。");
return false;
}
}
return true;
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop, false);
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
foreach (string s in files)
{
if (!File.Exists(s)) return;
}
e.Effect = DragDropEffects.Copy;
}
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop,false);
PathSplit f = new PathSplit(files, this);
f.ShowDialog();
}
}
}