How do I: Move a project file to another project folder

Hi

I have an IProjectFile instance that I want to move to another physical
location. IProjectFile.Move does not seem to do the trick and I get an
exception every time I call IProject.CreateFolder (IProject.CreateFile seem
to work fine).

I want something like:

IProjectFile projectFile = ...;
string newLocation = "C:\MyFolder";

project.MoveProjectFile(projectFile, newLocation);

Any help would be appreciated.

Thanks
--Jakob

1 comment
Comment actions Permalink

Hello Jakob,

yes, unfortunately, IProjectFolder.CreateFolder doesn't work under Visual
Studio, and IProjectItem.Move doesn't work as you likely
expect it - it doesn't update VS's project model. You can, however,
use VS's ProjectItems.AddFolder to add a new folder to a project. I assume
that the target folder is to be created
under the same project. First, you will need to reference VS Interop
assemblies (Microsoft.VisualStudio.Shell.Interop) and EnvDTE. If I understand
correctly your scenario,
the pseudocode looks like this:

IProjectFile file; // the original file you want to move
VSProjectInfo projectInfo = ProjectModelSynchronizer.GetInstance(file.GetSolution()).GetProjectInfoByProject(file.GetProject());
if (projectInfo != null)
{
EnvDTE.Project project = projectInfo.GetExtProject();
if (project != null)
{
ProjectItem newFolder = project.ProjectItem.AddFolder ("NewFolder");
// see MSDN docs for this'
// find the corresponding folder in ReSharper project model
IProjectFolder newRSFolder = project.FindProjectItemByLocation(newFolder.get_FileNames(0));
}
}

However, I've no idea how to programmatically move a file using VS's API.
Probably if you explain your scenario in more details, we'll come up with
a better solution.



Dmitry Shaporenkov
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


Hi

I have an IProjectFile instance that I want to move to another
physical location. IProjectFile.Move does not seem to do the trick and
I get an exception every time I call IProject.CreateFolder
(IProject.CreateFile seem to work fine).

I want something like:

IProjectFile projectFile = ...;
string newLocation = "C:\MyFolder";
project.MoveProjectFile(projectFile, newLocation);

Any help would be appreciated.

Thanks
--Jakob



0

Please sign in to leave a comment.