Ejemplo n.º 1
0
 /// <summary>
 /// Copies or moves a file node (and the corresponding file, if applicable) to this directory,
 /// discarding its DependentUpon value.
 /// </summary>
 /// <param name="fileNode">The file node to copy or move.</param>
 /// <param name="performMove">true to move the file node, false to copy it.</param>
 public void CopyFileHere(FileNode node, bool performMove)
 {
     if (node.FileNodeStatus == FileNodeStatus.None)
     {
         AddExistingItemsToProject.CopyFile(node.FileName, this, false);
         if (performMove)
         {
             FileService.RemoveFile(node.FileName, false);
         }
     }
     else if (node.IsLink)
     {
         string          relFileName     = FileUtility.GetRelativePath(Project.Directory, node.FileName);
         FileNode        fileNode        = new FileNode(node.FileName, FileNodeStatus.InProject);
         FileProjectItem fileProjectItem = new FileProjectItem(Project, Project.GetDefaultItemType(node.FileName));
         fileProjectItem.Include = relFileName;
         fileProjectItem.SetEvaluatedMetadata("Link", Path.Combine(RelativePath, Path.GetFileName(node.FileName)));
         fileNode.ProjectItem = fileProjectItem;
         fileNode.InsertSorted(this);
         ProjectService.AddProjectItem(Project, fileProjectItem);
         if (performMove)
         {
             ProjectService.RemoveProjectItem(node.Project, node.ProjectItem);
             node.Remove();
         }
     }
     else
     {
         CopyFileHere(node.FileName, performMove);
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Copies or moves a file to this directory.
        /// </summary>
        /// <param name="fileName">The name of the file to copy or move.</param>
        /// <param name="performMove">true to move the file, false to copy it.</param>
        /// <param name="keepDependency">true to copy the DependentUpon value of the file to the target if possible, false to discard the DependentUpon value.</param>
        public void CopyFileHere(string fileName, bool performMove, bool keepDependency)
        {
            string shortFileName  = Path.GetFileName(fileName);
            string copiedFileName = Path.Combine(Directory, shortFileName);

            if (FileUtility.IsEqualFileName(fileName, copiedFileName))
            {
                return;
            }
            bool wasFileReplacement = false;

            if (File.Exists(copiedFileName))
            {
                if (!FileService.FireFileReplacing(copiedFileName, false))
                {
                    return;
                }
                if (AddExistingItemsToProject.ShowReplaceExistingFileDialog(null, copiedFileName, false) == AddExistingItemsToProject.ReplaceExistingFile.Yes)
                {
                    wasFileReplacement = true;
                    IViewContent viewContent = FileService.GetOpenFile(copiedFileName);
                    if (viewContent != null)
                    {
                        viewContent.WorkbenchWindow.CloseWindow(true);
                    }
                }
                else
                {
                    // don't replace file
                    return;
                }
            }

            FileProjectItem newItem       = AddExistingItemsToProject.CopyFile(fileName, this, true);
            IProject        sourceProject = Solution.FindProjectContainingFile(fileName);

            if (sourceProject != null)
            {
                string sourceDirectory         = Path.GetDirectoryName(fileName);
                bool   dependendElementsCopied = false;
                foreach (ProjectItem item in sourceProject.Items)
                {
                    FileProjectItem fileItem = item as FileProjectItem;
                    if (fileItem == null)
                    {
                        continue;
                    }
                    if (newItem != null && FileUtility.IsEqualFileName(fileItem.FileName, fileName))
                    {
                        fileItem.CopyMetadataTo(newItem);
                        if (!keepDependency)
                        {
                            // Prevent the DependentUpon from being copied
                            // because the referenced file is now in a different directory.
                            newItem.DependentUpon = String.Empty;
                        }
                    }
                    if (!string.Equals(fileItem.DependentUpon, shortFileName, StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }
                    string itemPath = Path.Combine(sourceProject.Directory, fileItem.VirtualName);
                    if (!FileUtility.IsEqualFileName(sourceDirectory, Path.GetDirectoryName(itemPath)))
                    {
                        continue;
                    }
                    // this file is dependend on the file being copied/moved: copy it, too
                    CopyFileHere(itemPath, performMove, true);
                    dependendElementsCopied = true;
                }
                if (dependendElementsCopied)
                {
                    RecreateSubNodes();
                }
            }
            if (performMove)
            {
                foreach (OpenedFile file in FileService.OpenedFiles)
                {
                    if (file.FileName != null &&
                        FileUtility.IsEqualFileName(file.FileName, fileName))
                    {
                        file.FileName = new FileName(copiedFileName);
                    }
                }
                FileService.RemoveFile(fileName, false);
            }
            if (wasFileReplacement)
            {
                FileService.FireFileReplaced(copiedFileName, false);
            }
        }
Ejemplo n.º 3
0
        public void CopyFileHere(string fileName, bool performMove)
        {
            string shortFileName  = Path.GetFileName(fileName);
            string copiedFileName = Path.Combine(Directory, shortFileName);

            if (FileUtility.IsEqualFileName(fileName, copiedFileName))
            {
                return;
            }
            bool wasFileReplacement = false;

            if (File.Exists(copiedFileName))
            {
                if (!FileService.FireFileReplacing(copiedFileName, false))
                {
                    return;
                }
                if (AddExistingItemsToProject.ShowReplaceExistingFileDialog(null, copiedFileName, false) == AddExistingItemsToProject.ReplaceExistingFile.Yes)
                {
                    wasFileReplacement = true;
                }
                else
                {
                    // don't replace file
                    return;
                }
            }

            FileProjectItem newItem       = AddExistingItemsToProject.CopyFile(fileName, this, true);
            IProject        sourceProject = Solution.FindProjectContainingFile(fileName);

            if (sourceProject != null)
            {
                string sourceDirectory         = Path.GetDirectoryName(fileName);
                bool   dependendElementsCopied = false;
                foreach (ProjectItem item in sourceProject.Items)
                {
                    FileProjectItem fileItem = item as FileProjectItem;
                    if (fileItem == null)
                    {
                        continue;
                    }
                    if (newItem != null && FileUtility.IsEqualFileName(fileItem.FileName, fileName))
                    {
                        fileItem.CopyMetadataTo(newItem);
                    }
                    if (!string.Equals(fileItem.DependentUpon, shortFileName, StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }
                    string itemPath = Path.Combine(sourceProject.Directory, fileItem.VirtualName);
                    if (!FileUtility.IsEqualFileName(sourceDirectory, Path.GetDirectoryName(itemPath)))
                    {
                        continue;
                    }
                    // this file is dependend on the file being copied/moved: copy it, too
                    CopyFileHere(itemPath, performMove);
                    dependendElementsCopied = true;
                }
                if (dependendElementsCopied)
                {
                    RecreateSubNodes();
                }
            }
            if (performMove)
            {
                foreach (IViewContent content in WorkbenchSingleton.Workbench.ViewContentCollection)
                {
                    if (content.FileName != null &&
                        FileUtility.IsEqualFileName(content.FileName, fileName))
                    {
                        content.FileName  = copiedFileName;
                        content.TitleName = shortFileName;
                    }
                }
                FileService.RemoveFile(fileName, false);
            }
            if (wasFileReplacement)
            {
                FileService.FireFileReplaced(copiedFileName, false);
            }
        }