Answered by:
VS2008 Add-in (Include an existing folder in a project)

Question
-
Hi all,
I'm trying to include an existing (potentially empty) folder in a project using a VS2008 add-in.
The folder(s) exist within the Project directory - the are simply not included within the Visual Studio project.
Attempting to add these folders via:
DTE.Solution.Item(x).ProjectItems.AddFolder("existing folder name")
Results in "A file or folder with the name XYZ already exists on disk at this location. Please choose another name...."
I know that I can force an include of this folder via Project.IncludeInProject, but that requires the correct folder to be selected within the Solution Explorer, which is simply not an option for me.
Is there another way of getting this folder included in my project?
Thanks,
Roy
Roy DrenkerSaturday, June 7, 2008 3:00 PM
Answers
-
The AddFolder will try to create a folder no matter the folder exists or not. And VS project model does not provide a method can let you add an existing folder. I'm afraid you have to temporarily rename the existing folder and after add a folder to the project items, move the files in the renamed folder to the new created folder.
And if you're sure that the folder is empty, just delete it and let VS create it for you will executing the AddFolder method.
Thanks!
Please remember to mark the replies as answers if they help and unmark them if they provide no help.Monday, June 9, 2008 7:09 AM -
Thanks for the feedback - I was afraid this would be the case.
I ended up working around the problem by creating a temporary empty file in that folder, and then adding the file to the project via AddFile(), which then automatically included the folder for me. I then did a ProjectItem.Remove() to remove the temporary file from the project, and then a File.Delete() to get rid of it.
Not the most elegant solution, but I'd be worried about locks or other possible issues if I tried to alter an existing folder on disk.
Roy
Roy Drenker- Marked as answer by Feng Chen Tuesday, June 10, 2008 4:20 AM
Monday, June 9, 2008 11:48 PM
All replies
-
The AddFolder will try to create a folder no matter the folder exists or not. And VS project model does not provide a method can let you add an existing folder. I'm afraid you have to temporarily rename the existing folder and after add a folder to the project items, move the files in the renamed folder to the new created folder.
And if you're sure that the folder is empty, just delete it and let VS create it for you will executing the AddFolder method.
Thanks!
Please remember to mark the replies as answers if they help and unmark them if they provide no help.Monday, June 9, 2008 7:09 AM -
Thanks for the feedback - I was afraid this would be the case.
I ended up working around the problem by creating a temporary empty file in that folder, and then adding the file to the project via AddFile(), which then automatically included the folder for me. I then did a ProjectItem.Remove() to remove the temporary file from the project, and then a File.Delete() to get rid of it.
Not the most elegant solution, but I'd be worried about locks or other possible issues if I tried to alter an existing folder on disk.
Roy
Roy Drenker- Marked as answer by Feng Chen Tuesday, June 10, 2008 4:20 AM
Monday, June 9, 2008 11:48 PM