承認処理。ハッシュを使って見たけれど実用的ではなさそう。
Main.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
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(); } } } |
UserPassword.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
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; namespace ファイル認証フォーム { public partial class UserPassword : Form { public bool approval_check; public string approval_user; public UserPassword(string entered_user = "") { InitializeComponent(); if (entered_user == "") { textBox1.Enabled = true; } else { textBox1.Enabled = false; textBox1.Text = entered_user; } } private void button1_Click(object sender, EventArgs e) { using (OleDbConnection con = new OleDbConnection(UserStrings.ds_approval)) { try { con.Open(); } catch { MessageBox.Show("データーベース接続にエラーが発生しました。"); approval_check = false; return; } string user_name = textBox1.Text.Replace("\'", "\'\'"); string password = textBox2.Text.Replace("\'", "\'\'"); try { string q = "select * from approval_tbl where user_name = '" + user_name + "';"; OleDbCommand cmd = new OleDbCommand(q, con); using (OleDbDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { if (dr["password"].ToString() == password) { approval_check = true; approval_user = user_name; this.Close(); return; } } MessageBox.Show("ユーザーが存在しません。"); approval_check = false; return; } } catch { MessageBox.Show("データ取得中にエラーが発生しました。"); approval_check = false; } } } } } |
UserStrings.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ファイル認証フォーム { class UserStrings { public static string ds_dat = "provider=microsoft.jet.oledb.4.0;data source=" + Application.StartupPath + @"\dat.mdb;"; public static string ds_approval = "provider=microsoft.jet.oledb.4.0;data source=" + Application.StartupPath + @"\approval.mdb;"; } } |
PathSplit.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
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 PathSplit : Form { Main fm; public PathSplit(string[] files, Main f) { fm = f; InitializeComponent(); string[] path = files[0].Split('\\'); if (path.Length >= 1) comboBox3.Text = path[path.Length - 1]; if (path.Length >= 2) comboBox2.Text = path[path.Length - 2]; if (path.Length >= 3) comboBox1.Text = path[path.Length - 3]; comboBox1.Items.AddRange(path); comboBox2.Items.AddRange(path); comboBox3.Items.AddRange(path); textBox1.Text = files[0]; using (FileStream fs = new FileStream(files[0],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")); } textBox2.Text = res.ToString(); } } private bool duplicate_path_chechk() { using (OleDbConnection con = new OleDbConnection(UserStrings.ds_dat)) { try { con.Open(); } catch { MessageBox.Show("データーベース接続にエラーが発生しました。"); return false; } string file_path = textBox1.Text.Replace("\'", "\'\'"); try { string q = "select * from dat_tbl where file_path = '" + file_path + "';"; OleDbCommand cmd = new OleDbCommand(q, con); using (OleDbDataReader dr = cmd.ExecuteReader()) { if (dr.Read()) { MessageBox.Show("既に登録済です。パスは重複できません。"); return false; } else { return true; } } } catch { MessageBox.Show("データ取得にエラーが発生しました。"); return false; } } } private void button1_Click(object sender, EventArgs e) { if (!duplicate_path_chechk()) { this.Close(); return; } using (OleDbConnection con = new OleDbConnection(UserStrings.ds_dat)) { try { con.Open(); } catch { MessageBox.Show("データーベース接続にエラーが発生しました。"); return; } string customer = comboBox1.Text.Replace("\'", "\'\'"); string document_type = comboBox2.Text.Replace("\'", "\'\'"); string file_name = comboBox3.Text.Replace("\'", "\'\'"); string file_path = textBox1.Text.Replace("\'","\'\'"); string md5 = textBox2.Text; string notes = textBox3.Text.Replace("\'", "\'\'"); string q = "insert into dat_tbl (customer,document_type,file_name,file_path,md5,updated,notes)" + " values ('" + customer + "','" + document_type + "','" + file_name + "','" + file_path + "','" + md5 + "','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "','" + notes + "');"; OleDbCommand cmd = new OleDbCommand(q,con); try { cmd.ExecuteNonQuery(); } catch { MessageBox.Show("正しく登録できませんでした。"); return; } MessageBox.Show("登録しました。"); this.Close(); fm.load_dgv(); } } } } |