示例#1
0
 public object Clone()
 {
     EnvironmentVariable variable = new EnvironmentVariable();
     variable.Name = this.name;
     variable.Value = this.Value;
     return variable;
 }
示例#2
0
        public VarImportCommand(TextBox txtVarName, string currentVarValues, string fileName, DgvHandler dgvHandler)
        {
            this.txtVarName = txtVarName;
            this.curVarValues = currentVarValues;
            this.variable = new EnvironmentVariable();
            EnvironmentSnapshotManager.LoadVariable(fileName, ref variable);
            this.dgvHandler = dgvHandler;

            GetValuesToImport();
        }
        public EnvironmentSnapshotManager()
        {
            EnvironmentVariableTarget usrTarget = EnvironmentVariableTarget.User;
            EnvironmentSnapshot usrSnapshot = new EnvironmentSnapshot("[Current]", usrTarget);
            foreach (DictionaryEntry entry in EnvironmentVariableManager.GetEnvironmentVariables(usrTarget))
            {
                EnvironmentVariable variable = new EnvironmentVariable();
                variable.Name = entry.Key.ToString();
                variable.Value = entry.Value.ToString();
                usrSnapshot.Variables.Add(variable);
            }
            usrSnapshots.Add(usrSnapshot);
            try
            {
                foreach (var file in Directory.GetFiles(appFolder + "\\snapshots\\usr", "*.env", SearchOption.TopDirectoryOnly))
                {
                    EnvironmentSnapshot snapshot = new EnvironmentSnapshot(Path.GetFileNameWithoutExtension(file), usrTarget);
                    usrSnapshots.Add(snapshot);
                }
            }
            catch (DirectoryNotFoundException)
            {
                Directory.CreateDirectory(appFolder + "\\snapshots\\usr");
            }

            EnvironmentVariableTarget sysTarget = EnvironmentVariableTarget.Machine;
            EnvironmentSnapshot sysSnapshot = new EnvironmentSnapshot("[Current]", sysTarget);
            foreach (DictionaryEntry entry in EnvironmentVariableManager.GetEnvironmentVariables(sysTarget))
            {
                EnvironmentVariable variable = new EnvironmentVariable();
                variable.Name = entry.Key.ToString();
                variable.Value = entry.Value.ToString();
                sysSnapshot.Variables.Add(variable);
            }
            sysSnapshots.Add(sysSnapshot);
            try
            {
                foreach (var file in Directory.GetFiles(appFolder + "\\snapshots\\sys", "*.env", SearchOption.TopDirectoryOnly))
                {
                    EnvironmentSnapshot snapshot = new EnvironmentSnapshot(Path.GetFileNameWithoutExtension(file), EnvironmentVariableTarget.Machine);
                    sysSnapshots.Add(snapshot);
                }
            }
            catch (DirectoryNotFoundException)
            {
                Directory.CreateDirectory(appFolder + "\\snapshots\\sys");
            }
        }
示例#4
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;
        }
 public static void SaveVariable(string file, ref EnvironmentVariable variable)
 {
     try
     {
         FileStream stream = File.Create(file);
         XmlSerializer xmlSerializer = new XmlSerializer(variable.GetType());
         xmlSerializer.Serialize(stream, variable);
         stream.Close();
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
 public static void LoadVariable(string file, ref EnvironmentVariable variable)
 {
     try
     {
         FileStream stream = File.OpenRead(file);
         XmlSerializer xmlSerializer = new XmlSerializer(variable.GetType());
         variable = (EnvironmentVariable)xmlSerializer.Deserialize(stream);
         stream.Close();
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
示例#7
0
 private void ExportEnvironmentVariable()
 {
     try
     {
         saveFileDialog.InitialDirectory = Environment.SpecialFolder.MyDocuments.ToString();
         if (saveFileDialog.ShowDialog() == DialogResult.OK)
         {
             EnvironmentVariable envVar = new EnvironmentVariable();
             envVar.Name = txtVariableName.Text;
             envVar.Value = EnvironmentVariableValue().ToString();
             EnvironmentSnapshotManager.SaveVariable(saveFileDialog.FileName, ref envVar);
             MessageBox.Show( "'" + txtVariableName.Text
                 + "' successfully exported to " + saveFileDialog.FileName
                 + " file.", "Export Success!", MessageBoxButtons.OK, MessageBoxIcon.Information );
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show( ex.Message, "Error!" );
     }
 }