Esempio n. 1
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);
            }
        }
Esempio n. 2
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);
            }
        }