Visual Studio Extensibility: Adding existing folders to a project
Asked Answered
F

5

5

I'm trying to use Visual Studio 2008's extensibility to write an addin that will create a project folder with various messages in it after parsing an interface. I'm having trouble at the step of creating/adding the folder, however. I've tried using

ProjectItem folder = 
item.ProjectItem.Collection.AddFolder(newDirectoryName, string.Empty); 

(item is my target file next to which I'm creating a folder with the same name but "Messages" appended to it) but it chokes when a folder already exists (no big surprise).

I tried deleting it if it already exists, such as:

DirectoryInfo dirInfo = new DirectoryInfo(newDirectoryParent + 
newDirectoryName); 
if (dirInfo.Exists) 
{
    dirInfo.Delete(true);
}

ProjectItem folder = 
item.ProjectItem.Collection.AddFolder(newDirectoryName, string.Empty); 

I can SEE that the folder gets deleted when in debug, but it still seems to think the folder is still there and dies on a folder already exists exception.

Any ideas???

Thanks.

AK

.... Perhaps the answer would lie in programmatically refreshing the project after the delete? How might this be done?

Fremitus answered 15/9, 2008 at 16:23 Comment(1)
any solution about it ? any answers was helpful ?Sidesaddle
N
4
ProjectItem pi = null;
var dir = Path.Combine(
      project.Properties.Item("LocalPath").Value.ToString(), SubdirectoryName);
if (Directory.Exists(dir))
    pi = target.ProjectItems.AddFromDirectory(dir);
else
    pi = target.ProjectItems.AddFolder(dir);

ProjectItems.AddFromDirectory will add the directory and everything underneath the directory to the project.

Noiseless answered 7/4, 2011 at 19:25 Comment(0)
F
3

Yup, that was it...

DirectoryInfo dirInfo = new DirectoryInfo(newDirectoryParent + newDirectoryName);

if (dirInfo.Exists)
{
    dirInfo.Delete(true);
    item.DTE.ExecuteCommand("View.Refresh", string.Empty);
}

ProjectItem folder = item.ProjectItem.Collection.AddFolder(newDirectoryName, string.Empty);

If there's a more elegant way of doing this, it would be much appreciated...

Thanks.

Fremitus answered 15/9, 2008 at 17:25 Comment(0)
C
2

This is my approach:

//Getting the current project
private DTE2 _applicationObject;
System.Array projs = (System.Array)_applicationObject.ActiveSolutionProjects;
Project proy=(Project)projs.GetValue(0);
//Getting the path
string path=proy.FullName.Substring(0,proy.FullName.LastIndexOf('\\'));
//Valitating if the path exists
bool existsDirectory= Directory.Exists(path + "\\Directory");
//Deleting and creating the Directory
if (existeClasses)
   Directory.Delete(path + "\\Directory", true);
Directory.CreateDirectory(path + "\\Directory");
//Including in the project
proy.ProjectItems.AddFromDirectory(path + "\\Directory");
Canny answered 28/6, 2011 at 20:54 Comment(0)
C
1

I am developing an extension for Visual Studio 2019 and had a similar issue. The question asked in the following page helped me out:

https://social.msdn.microsoft.com/Forums/en-US/f4a4f73b-3e13-40bf-99df-9c1bba8fe44e/include-existing-folder-path-as-project-item?forum=vsx

If the folder does not physically exist, you can use AddFolder(folderName). But if the folder is not included in the project while existing physically, you need to provide the full system path to the folder. (AddFolder(fullPath))

Cobos answered 15/11, 2020 at 15:16 Comment(1)
This was after all the only working solution for me, tried all other approaches before, also AddFromDirectory which didn't work for me.Allotropy
I
0

here's an idea i thought of because i've been using NAnt for so long and thought it might work.

Open the .csproj file in a text editor and add the directory as such:

<ItemGroup>
   <compile include="\path\rootFolderToInclude\**\*.cs" />
</ItemGroup>

if an "ItemGroup" already esists, that's fine. Just add it into an existing one. Visual studio won't really know how to edit this entry, but it will scan the whole directory.

edit to whatever you'd like.

Isomagnetic answered 30/7, 2009 at 20:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.