Beispiel #1
0
        public PackageFile AddFile(string filePath)
        {
            if (!File.Exists(filePath))
            {
                throw new ArgumentException("File does not exist.", "filePath");
            }

            var newFileName = System.IO.Path.GetFileName(filePath);

            if (ContainsFolder(newFileName))
            {
                PackageViewModel.UIServices.Show(Resources.FileNameConflictWithExistingDirectory, MessageLevel.Error);
                return(null);
            }

            var showingRemovedFile = false;

            if (ContainsFile(newFileName))
            {
                var confirmed = PackageViewModel.UIServices.Confirm(
                    Resources.ConfirmToReplaceExsitingFile_Title,
                    string.Format(CultureInfo.CurrentCulture, Resources.ConfirmToReplaceExsitingFile, newFileName),
                    isWarning: true);

                if (confirmed)
                {
                    var part = this[newFileName] as PackageFile;
                    showingRemovedFile = PackageViewModel.IsShowingFileContent(part);

                    // remove the existing file before adding the new one
                    RemoveChildByName(newFileName);
                }
                else
                {
                    return(null);
                }
            }

            var newTargetPath = Path + "\\" + newFileName;
            var physicalFile  = new DiskPackageFile(newTargetPath, filePath);
            var newFile       = new PackageFile(physicalFile, newFileName, this);

            Children.Add(newFile);
            newFile.IsSelected = true;
            IsExpanded         = true;
            PackageViewModel.NotifyChanges();

            if (showingRemovedFile)
            {
                PackageViewModel.ShowFileContent(newFile);
            }

            return(newFile);
        }
        private void WatchPhysicalFile(DiskPackageFile physicalFile)
        {
            var folderPath = System.IO.Path.GetDirectoryName(physicalFile.OriginalPath);
            var fileName   = System.IO.Path.GetFileName(physicalFile.OriginalPath);

            _watcher = new FileSystemWatcher(folderPath, fileName)
            {
                IncludeSubdirectories = false,
                EnableRaisingEvents   = true
            };

            _watcher.Changed += OnFileChanged;
            _watcher.Deleted += OnFileDeleted;
            _watcher.Renamed += OnFileDeleted;
        }
Beispiel #3
0
        public void AddFile(PackageFile file, bool makeCopy = false)
        {
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }

            if (Contains(file))
            {
                return;
            }

            PackagePart newFile;

            if (makeCopy)
            {
                string fileCopyPath;
                using (var originalFileStream = file.GetStream())
                {
                    fileCopyPath = FileHelper.CreateTempFile(file.Name, originalFileStream);
                }

                var newTargetPath = Path + "\\" + file.Name;
                var physicalFile  = new DiskPackageFile(newTargetPath, fileCopyPath);

                newFile = new PackageFile(physicalFile, file.Name, this);
            }
            else
            {
                // detach from current parent
                if (file.Parent != null)
                {
                    file.Parent.Detach(file);
                }

                newFile = file;
            }

            Attach(newFile);
            newFile.IsSelected = true;
            IsExpanded         = true;
            PackageViewModel.NotifyChanges();
        }