using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using System.Reflection; using AppRes = Eugen.ESystem.Windows.Forms.Properties.AppResources; using System.Xml.Linq; namespace Eugen.ESystem.Windows.Forms { public partial class DuoValueControl : UserControl { #region Version und Copyright // Keys und Copyright static string dllName = Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); //Den Programmnamen auslesen static string dllVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); static object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false); static string copyright = GenerateCopyright(); string icon = Application.StartupPath + "\\Info.bmp"; //Assembly Datum und Zeit static DateTime value = AssemblyDateTime(); string date = value.ToShortDateString(); string time = value.ToLongTimeString(); private static DateTime AssemblyDateTime() { var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; var buildDateTime = new DateTime(2000, 1, 1).Add(new TimeSpan(TimeSpan.TicksPerDay * version.Build + TimeSpan.TicksPerSecond * 2 * version.Revision)); //Tage seit dem 1. Januar 2000 und Sekunden seit Mitternacht, (multiplizier mit 2 ergibt das Original) return buildDateTime; } private static string CurrentYear() { DateTime dtn = DateTime.Now; return dtn.Year.ToString(); } private static string StartYear() { string startYear = ""; if (((AssemblyCopyrightAttribute)attributes[0]).Copyright.Contains("Copyright © ")) { startYear = ((AssemblyCopyrightAttribute)attributes[0]).Copyright.Replace("Copyright © ", ""); while (startYear.StartsWith(" ")) { startYear = startYear.Remove(0, 1); } startYear = startYear.Remove(startYear.IndexOf(" ")); return startYear; } else { DateTime dtn = DateTime.Now; return dtn.Year.ToString(); } } private static string GenerateCopyright() { string startYear = StartYear(); string currentYear = CurrentYear(); if (startYear == currentYear) { return ((AssemblyCopyrightAttribute)attributes[0]).Copyright; } else { return ((AssemblyCopyrightAttribute)attributes[0]).Copyright.Replace(startYear, String.Concat(startYear, "-", currentYear)); } } #endregion #region DLL-Info /// /// Contains the name of the dll file. /// public static string DllName { private set; get; } /// /// Contains the version of the dll file. /// public static string DllVersion { private set; get; } /// /// Contains the copyright notice. /// public static string Copyright { private set; get; } /// /// Contains the name-, version- and copyright-information of the dll file. /// public static string[] DllInfo { private set; get; } private static void SetDllInfo() { //Name, Keys und Copyright setzen DllName = dllName; DllVersion = dllVersion; Copyright = copyright; } #endregion static DuoValueControl() { SetDllInfo(); //Name, Keys und Copyright der DLL setzen } /// /// Creates a control with which the values in an XML file can be edited /// public DuoValueControl() { InitializeComponent(); InitialDirectory = @"C:\"; Topic = "Topic"; StandardValue = 0; } #region Public values /// /// The password used for encryption. An empty value disables the Encrypt values switch /// public string Password { get { return password; } set { password = value; checkBoxEncryptValues.Enabled = !String.IsNullOrEmpty(Password); } } /// /// Defines or read the initial directory /// public string InitialDirectory { set; get; } /// /// XML file name. Must be specified mandatory /// public string XMLfileName { set; get; } /// /// Adjusts the labeling of the values control. An empty value forces the default labeling /// public string ValueControlName { get { return groupBoxValueControl.Text; } set { groupBoxValueControl.Text = value; groupBoxValueControl.Refresh(); } } /// /// Defines the Topic. An empty value forces the default labeling /// public string Topic { set; get; } /// /// Adjusts the labeling of the group designation. An empty value forces the default labeling /// public string GroupName { get { return labelGroup.Text; } set { labelGroup.Text = value; } } /// /// Adjusts the name of the group attribute used in the XML file. Required by the Load function /// public string Group { set; get; } /// /// Adjusts the name of the key attribute used in the XML file. Required by the Load function /// public string Key { set; get; } /// /// Adjusts the labeling of the table header designation for the key. An empty value forces the default labeling /// public string TableKeyName { get { return dataGridViewValues.Columns[0].HeaderText; } set { dataGridViewValues.Columns[0].HeaderText = value; } } /// /// Adjusts the labeling of the table header designation for the value. An empty value forces the default labeling /// public string TableValueName { get { return dataGridViewValues.Columns[1].HeaderText; } set { dataGridViewValues.Columns[1].HeaderText = value; } } /// /// Adjusts the labeling of the encrypt value checkBox. An empty value forces the default labeling /// public string CheckBoxEncryptValueText { get { return checkBoxEncryptValues.Text; } set { checkBoxEncryptValues.Text = value; checkBoxEncryptValues.Enabled = !String.IsNullOrEmpty(Password); } } /// /// Controls whether values are always encrypted. An empty value forces the default value (false) /// public bool AlwaysEncrypted { get { return alwaysEncrypted; } set { alwaysEncrypted = value; checkBoxEncryptValues.Enabled = !alwaysEncrypted; checkBoxEncryptValues.Checked = alwaysEncrypted; } } #endregion #region Private values private string password; private bool alwaysEncrypted = false; private bool standardValueSet = false; private int selectedRow = 0; private int rowIndexColored = 0; private int StandardValue { get; set; } #endregion private void textBoxGroup_TextChanged(object sender, EventArgs e) { Group = textBoxGroup.Text; buttonSave.Enabled = (!String.IsNullOrEmpty(textBoxGroup.Text)); } private void buttonUp_Click(object sender, EventArgs e) { if (dataGridViewValues.SelectedCells.Count > 0) { int index = dataGridViewValues.SelectedCells[0].OwningRow.Index; if (index > 0) { int col = dataGridViewValues.SelectedCells[0].OwningColumn.Index; DataGridViewRowCollection rows = dataGridViewValues.Rows; DataGridViewRow row = rows[index]; rows.Remove(row); rows.Insert(index - 1, row); dataGridViewValues.ClearSelection(); dataGridViewValues.Rows[index - 1].Cells[col].Selected = true; } } } private void buttonDown_Click(object sender, EventArgs e) { if (dataGridViewValues.SelectedCells.Count > 0) { int index = dataGridViewValues.SelectedCells[0].OwningRow.Index; if (index < dataGridViewValues.Rows.Count - 2) { int col = dataGridViewValues.SelectedCells[0].OwningColumn.Index; DataGridViewRowCollection rows = dataGridViewValues.Rows; DataGridViewRow row = rows[index]; rows.Remove(row); rows.Insert(index + 1, row); dataGridViewValues.ClearSelection(); dataGridViewValues.Rows[index + 1].Cells[col].Selected = true; } } } private void buttonLoad_Click(object sender, EventArgs e) { if (String.IsNullOrEmpty(XMLfileName)) { openFileDialog.Title = AppRes.dlgOpenTitle; openFileDialog.InitialDirectory = InitialDirectory; openFileDialog.FileName = ""; openFileDialog.Filter = AppRes.dlgFilter; //"xml files (*.xml)|*.xml|All files (*.*)|*.*" openFileDialog.FilterIndex = 1; openFileDialog.RestoreDirectory = true; if (openFileDialog.ShowDialog() == DialogResult.OK) { XMLfileName = openFileDialog.FileName; LoadXML(); } } else { if (File.Exists(XMLfileName)) { LoadXML(); } else { MessageBox.Show(AppRes.msgBoxTxt001, AppRes.msgBoxCapt001, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } } private void buttonSave_Click(object sender, EventArgs e) { if (String.IsNullOrEmpty(XMLfileName)) { saveFileDialog.Title = AppRes.dlgSaveTitle; saveFileDialog.InitialDirectory = InitialDirectory; saveFileDialog.Filter = AppRes.dlgFilter; //"xml files (*.xml)|*.xml|All files (*.*)|*.*" saveFileDialog.FilterIndex = 1; saveFileDialog.RestoreDirectory = true; if (saveFileDialog.ShowDialog() == DialogResult.OK) { SaveXML(); } } else { SaveXML(); } } private void dataGridViewValues_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Delete) { selectedRow = dataGridViewValues.SelectedCells[0].RowIndex + 1; //Wenn die Zeile gelöscht wird, dann die Zeilennummer merken } } private void dataGridViewValues_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e) { //Wenn die gelöschte Zeile den Standardwert enthielt, dann die WErte zurücksetzten, weil es keinen Standardwert mehr gibt if (selectedRow == rowIndexColored) { StandardValue = 0; //Es gibt keine ausgewählte Standardzeile standardValueSet = false; //es gibt keinen Standardwert rowIndexColored = 0; //Es gibt keine umgefärbte Zeile } } public void SetBasicValues() { if (!String.IsNullOrEmpty(Group)) { textBoxGroup.Text = Group; } } private void LoadXML() { checkBoxEncryptValues.Checked = false; var loadXML = new LoadXML(); loadXML.FileName = XMLfileName; loadXML.Load(); string[] values = new string[loadXML.Values.Length]; values = loadXML.Values; if (!String.IsNullOrEmpty(Group)) { loadXML.Group = Group; } for (int i = 0; i < values.Length; i++) { if (values[i].StartsWith("$$$$")) { values[i] = Decrypt(values[i], Password); checkBoxEncryptValues.Checked = true; //Wenn ein verschlüsseltes Element gefunden wurde, dann setzen } dataGridViewValues.Rows.Add(loadXML.Keys[i], values[i]); } dataGridViewValues.Refresh(); textBoxGroup.Text = loadXML.Group; //Gruppe setzen } private void SaveXML() { if (!Directory.Exists(Path.GetDirectoryName(XMLfileName))) { Directory.CreateDirectory(Path.GetDirectoryName(XMLfileName)); //Wenn das Verzeichnis nicht existiert, dann erzeugen } int dgvLength = dataGridViewValues.RowCount - 1; string[] keys = new string[dgvLength]; string value = ""; string[] values = new string[dgvLength]; for (int i = 0; i < dgvLength; i++) { if (checkBoxEncryptValues.Checked) { value = Encrypt(dataGridViewValues[1, i].Value.ToString(), Password); //Verschlüsselt } else { value = dataGridViewValues[1, i].Value.ToString(); //Unverschlüsselt } keys[i] = dataGridViewValues[0, i].Value.ToString(); ; values[i] = value; } //Speichern var saveXML = new SaveXML(); saveXML.FileName = XMLfileName; saveXML.Topic = "NX-Portal"; //z.B. "NX-Portal" saveXML.Group = textBoxGroup.Text; //z.B. "NX 1953" saveXML.Keys = keys; //z.B. "UGII_ENV" saveXML.Values = values; //z.B. "M:\ug\NX1953_NXTC\global\00_DEFAULTS\UGII\ugii_env.dat" saveXML.Save(); } private string Encrypt(string clearText, string password) { var crypt = new Cryption(); return String.Concat("$$$$", crypt.EncryptString(clearText, password)); } private string Decrypt(string encryptedText, string password) { var crypt = new Cryption(); return crypt.DecryptString(encryptedText.Replace("$$$$", ""), password); } public void SetDialogWidth(int width) { if (width >= this.MinimumSize.Width && width <= this.MaximumSize.Width) { this.Width = width; int dif = width - 316; if (dif > 0) { dataGridViewValues.Columns[0].Width += dif / 2; } } } } }