private void CreateSnapshot(ListBox lb, string name) { try { if (name.Length > 0) { EnvironmentSnapshot snapshot = new EnvironmentSnapshot(name, (EnvironmentVariableTarget)lb.Tag); foreach (DictionaryEntry entry in EnvironmentVariableManager.GetEnvironmentVariables((EnvironmentVariableTarget)lb.Tag)) { EnvironmentVariable variable = new EnvironmentVariable(); variable.Name = entry.Key.ToString(); variable.Value = entry.Value.ToString(); snapshot.Variables.Add(variable); } if (snapshotManager.AppendSnapshot(snapshot)) { lb.Items.Add(snapshot.Name); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error!"); } }
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"); } }