/// <summary> /// 更新文件夹加密进度 /// </summary> public void RefreshDirProgress(int max, int value) { InvokeDelegate invokeDelegate = delegate() { pbDir.Maximum = max; pbDir.Value = value; lblProgressDir.Text = value * 100 / max + "%"; }; InvokeUtil.Invoke(this, invokeDelegate); }
/// <summary> /// 重新加密 /// </summary> public void ReEncrypt() { Thread thread = new Thread(new ParameterizedThreadStart(delegate(object obj) { try { InvokeDelegate invokeDelegate = delegate() { pbDir.Value = 0; lblProgressDir.Text = "0%"; pbFile.Value = 0; lblProgressFile.Text = "0%"; pbDir.Visible = true; lblProgressDir.Visible = true; pbFile.Visible = false; lblProgressFile.Visible = false; lblShowPath.Text = "加密文件夹:" + Application.StartupPath; lblShowPath.Visible = true; DisableBtns(); }; InvokeUtil.Invoke(this, invokeDelegate); DateTime t1 = DateTime.Now; DirectoryEncrypt.EncryptCurrentDirectory(Application.StartupPath, txtPwd.Text, RefreshDirProgress, RefreshFileProgress); DateTime t2 = DateTime.Now; string t = t2.Subtract(t1).TotalSeconds.ToString("0.00"); if (MessageBox.Show("加密成功,耗时" + t + "秒", "提示") == DialogResult.OK) { invokeDelegate = delegate() { EnableBtns(); Application.Exit(); //加密成功后关闭程序 }; InvokeUtil.Invoke(this, invokeDelegate); } } catch (Exception ex) { if (MessageBox.Show("加密失败:" + ex.Message, "提示") == DialogResult.OK) { InvokeDelegate invokeDelegate = delegate() { EnableBtns(); }; InvokeUtil.Invoke(this, invokeDelegate); } } })); thread.Start(); }
/// <summary> /// 更新文件加密进度 /// </summary> public void RefreshFileProgress(int max, int value) { InvokeDelegate invokeDelegate = delegate() { if (max > 1) { pbFile.Visible = true; lblProgressFile.Visible = true; } else { pbFile.Visible = false; lblProgressFile.Visible = false; } pbFile.Maximum = max; pbFile.Value = value; lblProgressFile.Text = value * 100 / max + "%"; }; InvokeUtil.Invoke(this, invokeDelegate); }
//生成 private void BtnCreate_Click(object sender, EventArgs e) { new Thread(delegate() { try { var cm = ConfigurationManager.ConnectionStrings["DbContext"]; var dal = DalFactory.CreateDal(cm.ProviderName); var tableList = dal.GetAllTables(); const string strNamespace = "Entity"; #region 操作控件 InvokeDelegate invokeDelegate = delegate { btnCreate.Enabled = false; progressBar1.Visible = true; progressBar1.Minimum = 0; progressBar1.Maximum = tableList.Count; progressBar1.Value = 0; }; InvokeUtil.Invoke(this, invokeDelegate); #endregion var i = 0; foreach (var table in tableList) { var sb = new StringBuilder(); //var sbExt = new StringBuilder(); var columnList = dal.GetAllColumns(table["table_name"]); #region 原始Model //sb.Append("using System;\r\n"); //sb.Append("using System.Collections.Generic;\r\n"); //sb.Append("using System.ComponentModel.DataAnnotations;"); //sb.Append("\r\n"); sb.Append("namespace " + strNamespace + "\r\n"); sb.Append("{\r\n"); sb.Append(" /// <summary>\r\n"); sb.Append(" /// " + table["comments"] + "\r\n"); sb.Append(" /// </summary>\r\n"); //sb.Append(" [Serializable]\r\n"); //sb.Append(" public partial class " + table["table_name"] + "\r\n"); sb.Append(" public class " + table["table_name"] + "\r\n"); sb.Append(" {\r\n"); foreach (var column in columnList) { var dataType = dal.ConvertDataType(column); sb.Append(" /// <summary>\r\n"); sb.Append(" /// " + column["comments"] + "\r\n"); sb.Append(" /// </summary>\r\n"); if (column["constraint_type"] == "P") { sb.Append(" [System.Data.Objects.DataClasses.EdmScalarProperty(EntityKeyProperty = true, IsNullable = false)]\r\n"); } //sb.Append(" [IsDBField]\r\n"); sb.Append(" public " + dataType + " " + column["columns_name"] + " { get; set; }\r\n"); } sb.Append(" }\r\n"); sb.Append("}\r\n"); FileHelper.WriteFile(AppDomain.CurrentDomain.BaseDirectory + strNamespace, sb.ToString(), table["table_name"]); #endregion #region 扩展Model //sbExt.Append("using System;\r\n"); //sbExt.Append("using System.Collections.Generic;\r\n"); //sbExt.Append("using System.Linq;\r\n"); //sbExt.Append("\r\n"); //sbExt.Append("namespace " + strNamespace + "\r\n"); //sbExt.Append("{\r\n"); //sbExt.Append(" /// <summary>\r\n"); //sbExt.Append(" /// " + table["comments"] + "\r\n"); //sbExt.Append(" /// </summary>\r\n"); //sbExt.Append(" public partial class " + table["table_name"] + "\r\n"); //sbExt.Append(" {\r\n"); //sbExt.Append("\r\n"); //sbExt.Append(" }\r\n"); //sbExt.Append("}\r\n"); //FileHelper.WriteFile(Application.StartupPath + "\\extmodels", sbExt.ToString(), table["table_name"]); #endregion #region 操作控件 invokeDelegate = delegate { progressBar1.Value = ++i; }; InvokeUtil.Invoke(this, invokeDelegate); #endregion } #region 操作控件 invokeDelegate = delegate { btnCreate.Enabled = true; progressBar1.Visible = false; progressBar1.Value = 0; }; InvokeUtil.Invoke(this, invokeDelegate); #endregion MessageBox.Show(@"生成完成"); } catch (Exception ex) { MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace); } }).Start(); }
//生成 private void btnCreate_Click(object sender, EventArgs e) { new Thread(new ThreadStart(delegate() { try { IDal dal = DalFactory.CreateDal(ConfigurationManager.AppSettings["DBType"]); List <Dictionary <string, string> > tableList = dal.GetAllTables(); string strNamespace = ConfigurationManager.AppSettings["Namespace"]; string strClassTemplate = string.Empty; string strClassExtTemplate = string.Empty; string strFieldTemplate = string.Empty; Regex regField = new Regex(@"[ \t]*#field start([\s\S]*)#field end", RegexOptions.IgnoreCase); #region 操作控件 InvokeDelegate invokeDelegate = delegate() { btnCreate.Enabled = false; progressBar1.Visible = true; progressBar1.Minimum = 0; progressBar1.Maximum = tableList.Count; progressBar1.Value = 0; }; InvokeUtil.Invoke(this, invokeDelegate); #endregion #region 读取模板 strClassTemplate = FileHelper.ReadFile(Application.StartupPath + "\\Template\\class.txt"); strClassExtTemplate = FileHelper.ReadFile(Application.StartupPath + "\\Template\\class_ext.txt"); Match matchField = regField.Match(strClassTemplate); if (matchField.Success) { strFieldTemplate = matchField.Groups[1].Value.TrimEnd(' '); } #endregion int i = 0; foreach (Dictionary <string, string> table in tableList) //遍历表 { string tableName = table["table_name"].ToUpper(); StringBuilder sbFields = new StringBuilder(); List <Dictionary <string, string> > columnList = dal.GetAllColumns(tableName); #region 原始Model string strClass = strClassTemplate.Replace("#table_comments", table["comments"]); strClass = strClass.Replace("#table_name", tableName); foreach (Dictionary <string, string> column in columnList) //遍历字段 { string data_type = dal.ConvertDataType(column); string strField = strFieldTemplate.Replace("#field_comments", column["comments"]); if (column["constraint_type"] != "P") { strField = strField.Replace(" [IsId]\r\n", string.Empty); } strField = strField.Replace("#data_type", data_type); strField = strField.Replace("#field_name", column["columns_name"].ToUpper()); sbFields.Append(strField); } strClass = regField.Replace(strClass, sbFields.ToString()); FileHelper.WriteFile(Application.StartupPath + "\\Models", strClass, tableName); #endregion #region 扩展Model string strClassExt = strClassExtTemplate.Replace("#table_comments", table["comments"]); strClassExt = strClassExt.Replace("#table_name", tableName); FileHelper.WriteFile(Application.StartupPath + "\\ExtModels", strClassExt.ToString(), tableName); #endregion #region 操作控件 invokeDelegate = delegate() { progressBar1.Value = ++i; }; InvokeUtil.Invoke(this, invokeDelegate); #endregion } #region 操作控件 invokeDelegate = delegate() { btnCreate.Enabled = true; progressBar1.Visible = false; progressBar1.Value = 0; }; InvokeUtil.Invoke(this, invokeDelegate); #endregion MessageBox.Show("完成"); } catch (Exception ex) { MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace); } })).Start(); }
//生成 private void btnCreate_Click(object sender, EventArgs e) { new Thread(new ThreadStart(delegate() { IDal dal = DalFactory.CreateDal(ConfigurationManager.AppSettings["DBType"]); List <Dictionary <string, string> > tableList = dal.GetAllTables(); string strNamespace = ConfigurationManager.AppSettings["Namespace"]; #region 操作控件 InvokeDelegate invokeDelegate = delegate() { btnCreate.Enabled = false; progressBar1.Visible = true; progressBar1.Minimum = 0; progressBar1.Maximum = tableList.Count; progressBar1.Value = 0; }; InvokeUtil.Invoke(this, invokeDelegate); #endregion int i = 0; foreach (Dictionary <string, string> table in tableList) { StringBuilder sb = new StringBuilder(); StringBuilder sbExt = new StringBuilder(); List <Dictionary <string, string> > columnList = dal.GetAllColumns(table["table_name"]); #region 原始Model sb.Append("using System;\r\n"); sb.Append("using System.Collections.Generic;\r\n"); sb.Append("using System.Linq;\r\n"); sb.Append("\r\n"); sb.Append("namespace " + strNamespace + "\r\n"); sb.Append("{\r\n"); sb.Append(" /// <summary>\r\n"); sb.Append(" /// " + table["comments"] + "\r\n"); sb.Append(" /// </summary>\r\n"); sb.Append(" [Serializable]\r\n"); sb.Append(" public partial class " + table["table_name"] + "\r\n"); sb.Append(" {\r\n"); foreach (Dictionary <string, string> column in columnList) { string data_type = dal.ConvertDataType(column); sb.Append(" /// <summary>\r\n"); sb.Append(" /// " + column["comments"] + "\r\n"); sb.Append(" /// </summary>\r\n"); if (column["constraint_type"] == "P") { sb.Append(" [IsId]\r\n"); } sb.Append(" [IsDBField]\r\n"); sb.Append(" public " + data_type + " " + column["columns_name"] + " { get; set; }\r\n"); } sb.Append(" }\r\n"); sb.Append("}\r\n"); FileHelper.WriteFile(Application.StartupPath + "\\models", sb.ToString(), table["table_name"]); #endregion #region 扩展Model sbExt.Append("using System;\r\n"); sbExt.Append("using System.Collections.Generic;\r\n"); sbExt.Append("using System.Linq;\r\n"); sbExt.Append("\r\n"); sbExt.Append("namespace " + strNamespace + "\r\n"); sbExt.Append("{\r\n"); sbExt.Append(" /// <summary>\r\n"); sbExt.Append(" /// " + table["comments"] + "\r\n"); sbExt.Append(" /// </summary>\r\n"); sbExt.Append(" public partial class " + table["table_name"] + "\r\n"); sbExt.Append(" {\r\n"); sbExt.Append("\r\n"); sbExt.Append(" }\r\n"); sbExt.Append("}\r\n"); FileHelper.WriteFile(Application.StartupPath + "\\extmodels", sbExt.ToString(), table["table_name"]); #endregion #region 操作控件 invokeDelegate = delegate() { progressBar1.Value = ++i; }; InvokeUtil.Invoke(this, invokeDelegate); #endregion } #region 操作控件 invokeDelegate = delegate() { btnCreate.Enabled = true; progressBar1.Visible = false; progressBar1.Value = 0; }; InvokeUtil.Invoke(this, invokeDelegate); #endregion MessageBox.Show("完成"); })).Start(); }
private void btnDecryptThis_Click(object sender, EventArgs e) { #region 验证 if (txtPwd.Text == "") { MessageBox.Show("密码不能为空", "提示"); return; } #endregion if (MessageBox.Show(string.Format("确定解密当前文件夹?"), "提示", MessageBoxButtons.OKCancel) == DialogResult.Cancel) { return; } Thread thread = new Thread(new ParameterizedThreadStart(delegate(object obj) { try { InvokeDelegate invokeDelegate = delegate() { pbDir.Value = 0; lblProgressDir.Text = "0%"; pbFile.Value = 0; lblProgressFile.Text = "0%"; pbDir.Visible = true; lblProgressFile.Visible = true; pbFile.Visible = false; lblProgressFile.Visible = false; lblShowPath.Text = "解密文件夹:" + Application.StartupPath; lblShowPath.Visible = true; DisableBtns(); }; InvokeUtil.Invoke(this, invokeDelegate); DateTime t1 = DateTime.Now; DirectoryEncrypt.DecryptCurrentDirectory(Application.StartupPath, txtPwd.Text, RefreshDirProgress, RefreshFileProgress); DateTime t2 = DateTime.Now; string t = t2.Subtract(t1).TotalSeconds.ToString("0.00"); if (MessageBox.Show("解密成功,耗时" + t + "秒", "提示") == DialogResult.OK) { invokeDelegate = delegate() { EnableBtns(); if (Properties.Settings.Default.NotifyUse) { //解密成功后最小化程序 txtPwdCfm.Text = txtPwd.Text; this.WindowState = FormWindowState.Minimized; this.Visible = false; notifyIcon1.Visible = true; } else { Application.Exit(); } }; InvokeUtil.Invoke(this, invokeDelegate); } } catch (Exception ex) { if (MessageBox.Show("解密失败:" + ex.Message, "提示") == DialogResult.OK) { InvokeDelegate invokeDelegate = delegate() { EnableBtns(); tryCount++; if (tryCount == maxTryCount) { this.Close(); } }; InvokeUtil.Invoke(this, invokeDelegate); } } })); thread.Start(); }
private void btnEncrypt_Click(object sender, EventArgs e) { #region 验证 if (txtPwd.Text == "") { MessageBox.Show("密码不能为空", "提示"); return; } if (txtPwdCfm.Text == "") { MessageBox.Show("xwuser密码不能为空", "提示"); return; } #endregion if (openFileDialog1.ShowDialog() == DialogResult.OK) { Thread thread = new Thread(new ParameterizedThreadStart(delegate(object obj) { try { InvokeDelegate invokeDelegate = delegate() { pbFile.Value = 0; lblProgressFile.Text = "0%"; pbDir.Visible = false; lblProgressDir.Visible = false; pbFile.Visible = false; lblProgressFile.Visible = false; lblShowPath.Text = "加密文件:" + openFileDialog1.FileName; lblShowPath.Visible = true; DisableBtns(); }; InvokeUtil.Invoke(this, invokeDelegate); DateTime t1 = DateTime.Now; FileEncrypt.EncryptFile(openFileDialog1.FileName, txtPwd.Text, RefreshFileProgress); DateTime t2 = DateTime.Now; string t = t2.Subtract(t1).TotalSeconds.ToString("0.00"); if (MessageBox.Show("加密成功,耗时" + t + "秒", "提示") == DialogResult.OK) { invokeDelegate = delegate() { EnableBtns(); }; InvokeUtil.Invoke(this, invokeDelegate); } } catch (Exception ex) { if (MessageBox.Show("加密失败:" + ex.Message, "提示") == DialogResult.OK) { InvokeDelegate invokeDelegate = delegate() { EnableBtns(); }; InvokeUtil.Invoke(this, invokeDelegate); } } })); thread.Start(); } }
private void btnEncryptThis_Click(object sender, EventArgs e) { #region 验证 if (txtPwd.Text == "") { MessageBox.Show("密码不能为空", "提示"); return; } if (txtPwdCfm.Text == "") { MessageBox.Show("xwuser密码不能为空", "提示"); return; } #endregion if (MessageBox.Show(string.Format("确定加密当前文件夹?"), "提示", MessageBoxButtons.OKCancel) == DialogResult.Cancel) { return; } Thread thread = new Thread(new ParameterizedThreadStart(delegate(object obj) { try { InvokeDelegate invokeDelegate = delegate() { pbDir.Value = 0; lblProgressDir.Text = "0%"; pbFile.Value = 0; lblProgressFile.Text = "0%"; pbDir.Visible = true; lblProgressDir.Visible = true; pbFile.Visible = false; lblProgressFile.Visible = false; lblShowPath.Text = "加密文件夹:" + Application.StartupPath; lblShowPath.Visible = true; DisableBtns(); }; InvokeUtil.Invoke(this, invokeDelegate); DateTime t1 = DateTime.Now; DirectoryEncrypt.EncryptCurrentDirectory(Application.StartupPath, txtPwd.Text, RefreshDirProgress, RefreshFileProgress); DateTime t2 = DateTime.Now; string t = t2.Subtract(t1).TotalSeconds.ToString("0.00"); if (MessageBox.Show("加密成功,耗时" + t + "秒", "提示") == DialogResult.OK) { invokeDelegate = delegate() { EnableBtns(); Application.Exit(); //加密成功后关闭程序 }; InvokeUtil.Invoke(this, invokeDelegate); } } catch (Exception ex) { if (MessageBox.Show("加密失败:" + ex.Message, "提示") == DialogResult.OK) { InvokeDelegate invokeDelegate = delegate() { EnableBtns(); }; InvokeUtil.Invoke(this, invokeDelegate); } } })); thread.Start(); }
private void btnDecryptDir_Click(object sender, EventArgs e) { #region 验证 if (txtPwd.Text == "") { MessageBox.Show("密码不能为空", "提示"); return; } #endregion if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { if (MessageBox.Show(string.Format("确定解密文件夹{0}?", folderBrowserDialog1.SelectedPath), "提示", MessageBoxButtons.OKCancel) == DialogResult.Cancel) { return; } Thread thread = new Thread(new ParameterizedThreadStart(delegate(object obj) { try { InvokeDelegate invokeDelegate = delegate() { pbDir.Value = 0; lblProgressDir.Text = "0%"; pbFile.Value = 0; lblProgressFile.Text = "0%"; pbDir.Visible = true; lblProgressFile.Visible = true; pbFile.Visible = false; lblProgressFile.Visible = false; lblShowPath.Text = "解密文件夹:" + folderBrowserDialog1.SelectedPath; lblShowPath.Visible = true; DisableBtns(); }; InvokeUtil.Invoke(this, invokeDelegate); DateTime t1 = DateTime.Now; DirectoryEncrypt.DecryptDirectory(folderBrowserDialog1.SelectedPath, txtPwd.Text, RefreshDirProgress, RefreshFileProgress); DateTime t2 = DateTime.Now; string t = t2.Subtract(t1).TotalSeconds.ToString("0.00"); if (MessageBox.Show("解密成功,耗时" + t + "秒", "提示") == DialogResult.OK) { invokeDelegate = delegate() { EnableBtns(); }; InvokeUtil.Invoke(this, invokeDelegate); } } catch (Exception ex) { if (MessageBox.Show("解密失败:" + ex.Message, "提示") == DialogResult.OK) { InvokeDelegate invokeDelegate = delegate() { EnableBtns(); tryCount++; if (tryCount == maxTryCount) { this.Close(); } }; InvokeUtil.Invoke(this, invokeDelegate); } } })); thread.Start(); } }