public static void Replace(ReplaceArgs args) { CheckCurrentLocationIsTemp(); using (var parentProcess = GetProcessByLocation(args.OriginalLocation)) { if (parentProcess != null) { const int waitTimeout = 5000; if (!parentProcess.WaitForExit(waitTimeout)) throw new TimeoutException("Parent process is still running"); } } try { if (File.Exists(args.OriginalLocation)) File.Delete(args.OriginalLocation); } catch (Exception ex) { var message = string.Format("Unable to delete file {0}", args.OriginalLocation); throw new IOException(message, ex); } try { File.Move(args.TempLocation, args.OriginalLocation); } catch (Exception ex) { var message = string.Format("Unable to move file from {0} to {1}", args.TempLocation, args.OriginalLocation); throw new IOException(message, ex); } }
private static void InvokeReplace() { var originalLocation = Location.GetOriginalLocation(); var tempLocation = Location.GetTempLocation(); try { var replaceArgs = new ReplaceArgs { OriginalLocation = originalLocation, TempLocation = tempLocation }; Process.Start(tempLocation, replaceArgs.ToString()); } catch (Exception ex) { var message = string.Format("Unable to start executable {0}", originalLocation); try { File.Delete(tempLocation); } catch { } throw new InvalidOperationException(message, ex); } }