private void button1_Click(object sender, RoutedEventArgs e) { string pwdregex = "^[a-zA-Z0-9]{6,12}$"; System.Text.RegularExpressions.Regex regPwd = new System.Text.RegularExpressions.Regex(pwdregex); if (!regPwd.IsMatch(PasswordBox1.Password.Trim())) { label_warning.Content = "提示:密码由6-12位数字或字母组成!"; } else if (PasswordBox1.Password.Equals(PasswordBox2.Password)) { MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] str = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(PasswordBox1.Password)); string pwd = string.Empty; for (int i = 0; i < str.Length; i++) { pwd += Convert.ToString(str[i], 16).PadLeft(2, '0'); } try { using (StockManageDataContext smDataContext = new StockManageDataContext()) { string usrname = (this.Parent as LoginWindow).User; var s = smDataContext.Users.Single(c => c.UserName == usrname); s.Password = pwd; smDataContext.SubmitChanges(); MessageBox.Show("密码添加成功!"); this.NavigationService.Navigate(new Uri("LoginPage.xaml", UriKind.Relative)); } } catch (Exception ex) { MessageBox.Show(ex.Message); label_warning.Content = "提示:密码修改失败!"; } } else { label_warning.Content = "提示:两次输入的密码不同!"; } }