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?

Andrew
  • 192
  • 1
  • 9

5 Answers5

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.

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.

Andrew
  • 192
  • 1
  • 9
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");
isaacfi
  • 160
  • 1
  • 8
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.

0

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))

Mostafa F.Rad
  • 158
  • 1
  • 7