private void RenameAllSelectedDevices(MenuSelectionProvider menuSelectionProvider) { IEnumerable <object> menuSelction = menuSelectionProvider.GetSelection(); using (var fileStream = new FileStream(_traceFilePath, FileMode.Append)) { Trace.Listeners.Add(new TextWriterTraceListener(fileStream) { TraceOutputOptions = TraceOptions.DateTime }); Trace.AutoFlush = true; using (var exclusiveAccess = _tiaPortal.ExclusiveAccess("Renaming blocks...")) { var project = _tiaPortal.Projects.First(); using (var transaction = exclusiveAccess.Transaction(project, "Renaming blocks")) { List <Utility.UDeviceGroup> deviceGroups = new List <Utility.UDeviceGroup>(); bool replaceClicked = false; using (Form owner = Util.GetForegroundWindow()) { if (ShowMyDialogBox(owner) == DialogResult.OK) { replaceClicked = true; } } if (replaceClicked & !_Find.Equals(_Replace)) { foreach (Device device in menuSelction) { device.Name = device.Name.Replace(_Find, _Replace); foreach (DeviceItem deviceItem in device.DeviceItems) { deviceItem.Name = deviceItem.Name.Replace(_Find, _Replace); } } } using (Form owner = Util.GetForegroundWindow()) { MessageBox.Show(owner, "Completed Renaming"); } if (transaction.CanCommit) { transaction.CommitOnDispose(); } } } Trace.Close(); } DeleteEmptyTraceFile(); }
private void RenameAllSelectedPlcBlockGroups(MenuSelectionProvider <PlcBlockUserGroup> menuSelectionProvider) { using (var fileStream = new FileStream(_traceFilePath, FileMode.Append)) { Trace.Listeners.Add(new TextWriterTraceListener(fileStream) { TraceOutputOptions = TraceOptions.DateTime }); Trace.AutoFlush = true; bool replaceClicked = false; using (Form owner = Util.GetForegroundWindow()) { DialogResult result = ShowMyDialogBox(owner); if (result == DialogResult.OK) { replaceClicked = true; } } if (replaceClicked & !_Find.Equals(_Replace)) { Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); using (var exclusiveAccess = _tiaPortal.ExclusiveAccess("Renaming folder and contents from: " + _Find + " to: " + _Replace)) { var project = _tiaPortal.Projects.First(); var menuSelection = menuSelectionProvider.GetSelection(); var myUniqueFolderName = $@"{Guid.NewGuid()}"; bool TransactionSuccess = false; string path = Path.Combine(Environment.ExpandEnvironmentVariables("%TEMP%"), AppDomain.CurrentDomain.FriendlyName, myUniqueFolderName); Directory.CreateDirectory(path); List <BlockGroup> blockGroups = new List <BlockGroup>(); using (var transaction = exclusiveAccess.Transaction(project, "Renaming folder and contents from: " + _Find + " to: " + _Replace)) { try { foreach (PlcBlockUserGroup PlcBlockUserGroup in menuSelection) { blockGroups.Add(new BlockGroup(PlcBlockUserGroup, myUniqueFolderName)); } foreach (BlockGroup blockGroup in blockGroups) { RenameBlockGroup(blockGroup); } } catch (Exception ex) { Trace.TraceError("Exception during rename:" + Environment.NewLine + ex + Environment.NewLine + ex.TargetSite); return; } if (transaction.CanCommit) { TransactionSuccess = true; transaction.CommitOnDispose(); } } if (TransactionSuccess) { foreach (BlockGroup blockGroup in blockGroups) { blockGroup.Compile(); blockGroup.RefreshGroup(); if (blockGroup.IsChangeable) { blockGroup.Rename(_Find, _Replace); } } } using (Form owner = Util.GetForegroundWindow()) { MessageBox.Show(owner, "Completed Renaming"); } } TimeSpan ts = stopWatch.Elapsed; string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); Trace.TraceInformation("RunTime " + elapsedTime); } Trace.Close(); } DeleteEmptyTraceFile(); }