Esempio n. 1
0
        public FrmEditEnvVar(ref EnvironmentSnapshot snapshot, EnvironmentVariable variable)
        {
            InitializeComponent();
            this.snapshot          = snapshot;
            this.variable          = variable;
            this.MinimumSize       = new Size(327, 439);
            dgvValuesList.TabIndex = 0;
            LoadSettings();
            txtVariableName.CausesValidation = false;
            dgvHandler = new DgvHandler(ref dgvValuesList);

            // set default icon
            dgvValuesList_UserAddedRow(null, null);

            this.txtVariableName.Text = variable.Name;
            this.validator            = new EnvironmentValueValidator();

            if (txtVariableName.Text.Length != 0)
            {
                // Check if we are editing variable
                LoadEnvironmentVariableValues();
            }

            // Set form title
            this.Text = (txtVariableName.Text.Length != 0
                ? "Edit" : "New") + " "
                        + (this.snapshot.Target == EnvironmentVariableTarget.Machine
                ? "System" : "User") + " Variable";

            #region Create DgvHandler Commands
            commandsList = new CommandStack();
            dgvHandler.SetCurrentCell(0);
            editVarNameCommand = new EditVarNameCommand(txtVariableName);
            #endregion DgvHandler Commands

            // disable buttons
            SetBtnState();
            txtVariableName.CausesValidation = true;
            isVarNameChanged = false;

            // Open/Save File dialogs
            openFileDialog.Filter     = FILE_FILTER;
            openFileDialog.DefaultExt = DEFAULT_FILTER_EXTENSION;
            saveFileDialog.Filter     = FILE_FILTER;
            saveFileDialog.DefaultExt = DEFAULT_FILTER_EXTENSION;

            if (EnvironmentVariableManager.IsElevated)
            {
                btnSave.Image = Resources.Save;
            }
            else
            {
                btnSave.Image = Resources.shield_uac;
            }
        }
Esempio n. 2
0
        private void LocateInWindowsExplorer( )
        {
            EnvironmentValueValidator validator = new EnvironmentValueValidator();
            string varValue = string.Empty;

            foreach (DataGridViewRow row in dgvValuesList.SelectedRows)
            {
                if (row.Index != dgvValuesList.Rows.Count - 1)
                {
                    DataGridViewCell cell = row.Cells[1];
                    varValue = (cell.Value.ToString().Contains("%")) ? cell.ToolTipText : cell.Value.ToString();
                    switch (validator.ValueType(varValue))
                    {
                    case EnvironmentValueType.Folder:
                    {           // Open Folder in Windows Explorer
                        LocateInWindowsExplorer(varValue);
                    }
                    break;

                    case EnvironmentValueType.File:
                    {           // Select File in Windows Explorer
                        LocateInWindowsExplorer("/select," + varValue);
                    }
                    break;

                    case EnvironmentValueType.Error:
                    {           // Select existing folder in the path
                        string parentDir = Path.GetDirectoryName(varValue);
                        while (!Directory.Exists(parentDir))
                        {
                            parentDir = Path.GetDirectoryName(parentDir);
                        }
                        // Open Folder in Explorer
                        LocateInWindowsExplorer(parentDir);
                    }
                    break;

                    default:
                        // TODO: Remove for multiple rows
                        MessageBox.Show("Nothing to locate in Windows Explorer", "Validation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        break;
                    }
                }
            }
        }
Esempio n. 3
0
        private void FillEnvironmentVariables(DataGridView dgv, EnvironmentSnapshot snapshot)
        {
            int currentRowIndex = (dgv.CurrentRow != null ? dgv.CurrentRow.Index : 0);

            dgv.Rows.Clear();
            if (snapshot.Variables.Count == 0)
            {
                return;
            }
            EnvironmentValueValidator validator = new EnvironmentValueValidator();
            int rowIndex = 0;

            foreach (var variable in snapshot.Variables)
            {
                string   key   = variable.Name;
                string   value = variable.Value;
                string[] row   = { key, value };
                rowIndex = dgv.Rows.Add(row);
                if (!validator.Validate(value))
                {
                    dgv.Rows[rowIndex].Cells[0].Style.ForeColor = Color.Red;
                    dgv.Rows[rowIndex].Cells[1].Style.ForeColor = Color.Red;
                }
            }

            dgv.Sort(dgv.Columns[0], ListSortDirection.Ascending);

            try
            {
                dgv.CurrentCell = dgv[0, currentRowIndex];
                dgv.FirstDisplayedScrollingRowIndex = currentRowIndex;
            }
            catch
            {   // if row was deleted this will set it to first one
                // TODO: Implement this by searching for var name in the grid.
                // Catching Exceptions makes program slow
                dgv.CurrentCell = dgv[0, 0];
                dgv.FirstDisplayedScrollingRowIndex = 0;
            }
            int widthCol0 = dgv.Columns[0].Width;

            dgv.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
            dgv.Columns[0].Width        = widthCol0;
        }