views:

2072

answers:

2

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?

+2  A: 

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.

Andrew
A: 

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.