in Nodejs/Product/Nodejs/SharedProject/ProjectNode.CopyPaste.cs [1392:1554]
public override void DoAddition(ref bool? overwrite)
{
var newPath = Path.Combine(this.TargetFolder, this.NewFileName);
DirectoryInfo dirInfo = null;
try
{
dirInfo = Directory.CreateDirectory(this.TargetFolder);
}
catch (ArgumentException)
{
}
catch (UnauthorizedAccessException)
{
}
catch (IOException)
{
}
catch (NotSupportedException)
{
}
if (dirInfo == null)
{
//Something went wrong and we failed to create the new directory
// Inform the user and cancel the addition
Utilities.ShowMessageBox(
this.Project.Site,
SR.GetString(SR.FolderCannotBeCreatedOnDisk, CommonUtils.GetFileOrDirectoryName(this.TargetFolder)),
null,
OLEMSGICON.OLEMSGICON_CRITICAL,
OLEMSGBUTTON.OLEMSGBUTTON_OK,
OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
return;
}
if (this.DropEffect == DropEffect.Move && Utilities.IsSameComObject(this.Project, this.SourceHierarchy))
{
// we are doing a move, we need to remove the old item, and add the new.
// This also allows us to have better behavior if the user is selectively answering
// no to files within the hierarchy. We can do the rename of the individual items
// which the user opts to move and not touch the ones they don't. With a cross
// hierarchy move if the user answers no to any of the items none of the items
// are removed from the source hierarchy.
var fileNode = this.Project.FindNodeByFullPath(this.SourceMoniker);
Debug.Assert(fileNode is FileNode);
this.Project.ItemsDraggedOrCutOrCopied.Remove(fileNode); // we don't need to remove the file after Paste
if (File.Exists(newPath))
{
// we checked before starting the copy, but somehow a file has snuck in. Could be a race,
// or the user could have cut and pasted 2 files from different folders into the same folder.
bool shouldOverwrite;
if (overwrite == null)
{
if (!PromptOverwriteFile(Path.GetFileName(newPath), out var dialog))
{
// user cancelled
fileNode.ExpandItem(EXPANDFLAGS.EXPF_UnCutHighlightItem);
throw new CancelPasteException();
}
if (dialog.AllItems)
{
overwrite = dialog.ShouldOverwrite;
}
shouldOverwrite = dialog.ShouldOverwrite;
}
else
{
shouldOverwrite = overwrite.Value;
}
if (!shouldOverwrite)
{
fileNode.ExpandItem(EXPANDFLAGS.EXPF_UnCutHighlightItem);
return;
}
var existingNode = this.Project.FindNodeByFullPath(newPath);
if (existingNode != null)
{
existingNode.Remove(true);
}
else
{
File.Delete(newPath);
}
}
var file = fileNode as FileNode;
file.RenameInStorage(fileNode.Url, newPath);
file.RenameFileNode(fileNode.Url, newPath);
this.Project.Tracker.OnItemRenamed(this.SourceMoniker, newPath, VSRENAMEFILEFLAGS.VSRENAMEFILEFLAGS_NoFlags);
}
else
{
// we are copying and adding a new file node
File.Copy(this.SourceMoniker, newPath, true);
// best effort to reset the ReadOnly attribute
try
{
File.SetAttributes(newPath, File.GetAttributes(newPath) & ~FileAttributes.ReadOnly);
}
catch (ArgumentException)
{
}
catch (UnauthorizedAccessException)
{
}
catch (IOException)
{
}
var existing = this.Project.FindNodeByFullPath(newPath);
if (existing == null)
{
var fileNode = this.Project.CreateFileNode(newPath);
if (StringComparer.OrdinalIgnoreCase.Equals(this.TargetFolder, this.Project.FullPathToChildren))
{
this.Project.AddChild(fileNode);
}
else
{
var targetFolder = this.Project.CreateFolderNodes(this.TargetFolder);
//If a race occurrs simply treat the source as a non-included item
var wasMemberItem = false;
var sourceItem = this.Project.FindNodeByFullPath(this.SourceMoniker);
if (sourceItem != null)
{
wasMemberItem = !sourceItem.IsNonMemberItem;
}
if (wasMemberItem && targetFolder.IsNonMemberItem)
{
// dropping/pasting folder into non-member folder, non member folder
// should get included into the project.
ErrorHandler.ThrowOnFailure(targetFolder.IncludeInProject(false));
}
targetFolder.AddChild(fileNode);
if (!wasMemberItem)
{
// added child by default is included,
// non-member copies are not added to the project
ErrorHandler.ThrowOnFailure(fileNode.ExcludeFromProject());
}
}
this.Project.tracker.OnItemAdded(fileNode.Url, VSADDFILEFLAGS.VSADDFILEFLAGS_NoFlags);
}
else if (existing.IsNonMemberItem)
{
// replacing item that already existed, just include it in the project.
existing.IncludeInProject(false);
}
}
}