Beispiel #1
0
        public void Save()
        {
            lock (_LockObject)
            {
                log.Debug("Attempting to save '" + DestinationName + "'...");

                if (DownloadData != null &&
                    DownloadData.Length > 0)
                {
                    // Overwrite the previous version of the file, if the update hasn't been cancelled
                    if (!IsCancelled)
                    {
                        // Notify our UI that we're about to replace files
                        if (_UpdateNotifier != null)
                        {
                            _UpdateNotifier.BeginFileSave(DestinationPath);
                        }

                        try
                        {
                            // Determine if the directory exists at the destination path
                            // NOTE: fixes bug #1760479 - Directories not created for updated files
                            string directoryName = Path.GetDirectoryName(DestinationPath);
                            if (!string.IsNullOrEmpty(directoryName) &&
                                !Directory.Exists(directoryName))
                            {
                                // The directory does not exist; create it!
                                Directory.CreateDirectory(directoryName);
                            }

                            // Overwrite the previous version of the file!
                            FileStream fs = new FileStream(
                                DestinationPath,
                                FileMode.Create,
                                FileAccess.Write);
                            fs.Write(DownloadData, 0, DownloadData.Length);
                            fs.Close();

                            // Notify that we've completed replacing the file
                            if (_UpdateNotifier != null)
                            {
                                _UpdateNotifier.EndFileSave(DestinationPath);
                            }

                            OnSaved();
                        }
                        catch (Exception ex)
                        {
                            if (_UpdateNotifier != null)
                            {
                                _UpdateNotifier.OnError(new ExceptionEventArgs(ex));
                            }
                        }
                    }
                    else
                    {
                        log.Warn("'" + DestinationPath + "' could not be saved!");
                    }
                }
                else
                {
                    log.Info("Using '" + DestinationName + "' from previous version.");
                }
            }
        }