Move project #266
Unanswered
ben-hamida
asked this question in
Q&A
Replies: 1 comment 1 reply
-
I think you were on the right track with IVsSolution solution = await VS.Services.GetSolutionAsync();
Project projectToMove = GetProjectToMove(); // Find the project that you want to move.
// Remember the old path and project file name.
string oldPath = Path.GetDirectoryName(projectToMove.FullPath);
string name = Path.GetFileName(projectToMove.FullPath);
// Remove the project from the solution.
projectToMove.GetItemInfo(out IVsHierarchy hierarchy, out _, out _);
solution.CloseSolutionElement((uint)__VSSLNCLOSEOPTIONS.SLNCLOSEOPT_DeleteProject, hierarchy, 0);
// Move the project directory on disk.
string newPath = oldPath + "_moved";
Directory.Move(oldPath, newPath);
// Add the existing project to the solution.
solution.CreateProject(
default,
Path.Combine(newPath, name), // Specifying a file name here and null for the next two parameters will add an existing project.
null,
null,
(uint)__VSCREATEPROJFLAGS.CPF_OPENFILE,
typeof(IVsHierarchy).GUID,
out IntPtr projectPtr
);
try
{
// Get the new IVsHierachy from the pointer.
IVsHierarchy newHierarchy = (IVsHierarchy)Marshal.GetUniqueObjectForIUnknown(projectPtr);
Project newProject = (Project)await SolutionItem.FromHierarchyAsync(newHierarchy, VSConstants.VSITEMID_ROOT);
// Do something with the new project.
await VS.MessageBox.ShowAsync(newProject.FullPath);
}
finally
{
Marshal.Release(projectPtr);
} Two things that might be problems for you:
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I want to be able to move a project from one directory to another while keeping the project path in the solution file up to date.
I tried manually moving the project on disk and then patching the solution file but that displays the File Modification Detected dialog, and that I want to avoid.
I tried implementing IVsPersistSolutionProps but I found no way of writing the project path.
After some searching I found the IVsSolution3.UpdateProjectFileLocationForUpgrade but it didn't seem to do anything. Same with IVsSolution.OnAfterRenameProject.
The last thing I tried was to remove the project with IVsSolution.CloseSolutionElement and then readding it with the new path using _Solution.AddFromFile, but the AddFromFile call threw an InvalidOperationException with the message 'This operation is not valid while a project lock is held.'. This solution doesn't feel right anyway, there could be project specific properties in the solution file that wouldn't be preserved.
Is there a way to update the project path in the solution?
Beta Was this translation helpful? Give feedback.
All reactions