<p>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 </p>

<pre><code>ProjectItem folder = 
item.ProjectItem.Collection.AddFolder(newDirectoryName, string.Empty); 
</code></pre>

<p>(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).</p>

<p>I tried deleting it if it already exists, such as: </p>

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

ProjectItem folder = 
item.ProjectItem.Collection.AddFolder(newDirectoryName, string.Empty); 
</code></pre>

<p>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. </p>

<p>Any ideas??? </p>

<p>Thanks. </p>

<p>AK </p>

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

[{'answer_id': 64901, 'author': 'Andrew', 'author_id': 8586, 'author_profile': 'https://Stackoverflow.com/users/8586', 'pm_score': 2, 'selected': False, 'text': '<p>Yup, that was it...</p>\n\n<pre><code>DirectoryInfo dirInfo = new DirectoryInfo(newDirectoryParent + newDirectoryName);\n\nif (dirInfo.Exists)\n{\n    dirInfo.Delete(true);\n    item.DTE.ExecuteCommand("View.Refresh", string.Empty);\n}\n\nProjectItem folder = item.ProjectItem.Collection.AddFolder(newDirectoryName, string.Empty);\n</code></pre>\n\n<p>If there\'s a more elegant way of doing this, it would be much appreciated...</p>\n\n<p>Thanks.</p>\n'}, {'answer_id': 1209338, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>here\'s an idea i thought of because i\'ve been using NAnt for so long and thought it might work. </p>\n\n<p>Open the .csproj file in a text editor and add the directory as such:</p>\n\n<pre><code>&lt;ItemGroup&gt;\n   &lt;compile include="\\path\\rootFolderToInclude\\**\\*.cs" /&gt;\n&lt;/ItemGroup&gt;\n</code></pre>\n\n<p>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.</p>\n\n<p>edit to whatever you\'d like. </p>\n'}, {'answer_id': 5586467, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<pre><code>ProjectItem pi = null;\nvar dir = Path.Combine(\n      project.Properties.Item("LocalPath").Value.ToString(), SubdirectoryName);\nif (Directory.Exists(dir))\n    pi = target.ProjectItems.AddFromDirectory(dir);\nelse\n    pi = target.ProjectItems.AddFolder(dir);\n</code></pre>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/envdte.projectitems.addfromdirectory%28v=vs.80%29.aspx" rel="nofollow">ProjectItems.AddFromDirectory</a> will add the directory and everything underneath the directory to the project.</p>\n'}, {'answer_id': 6512963, 'author': 'isaacfi', 'author_id': 819963, 'author_profile': 'https://Stackoverflow.com/users/819963', 'pm_score': 2, 'selected': False, 'text': '<p>This is my approach:</p>\n\n<pre><code>//Getting the current project\nprivate DTE2 _applicationObject;\nSystem.Array projs = (System.Array)_applicationObject.ActiveSolutionProjects;\nProject proy=(Project)projs.GetValue(0);\n//Getting the path\nstring path=proy.FullName.Substring(0,proy.FullName.LastIndexOf(\'\\\\\'));\n//Valitating if the path exists\nbool existsDirectory= Directory.Exists(path + "\\\\Directory");\n//Deleting and creating the Directory\nif (existeClasses)\n   Directory.Delete(path + "\\\\Directory", true);\nDirectory.CreateDirectory(path + "\\\\Directory");\n//Including in the project\nproy.ProjectItems.AddFromDirectory(path + "\\\\Directory");\n</code></pre>\n'}, {'answer_id': 64846113, 'author': 'Persian Brat', 'author_id': 10132738, 'author_profile': 'https://Stackoverflow.com/users/10132738', 'pm_score': 1, 'selected': False, 'text': '<p>I am developing an extension for Visual Studio 2019 and had a similar issue. The question asked in the following page helped me out:</p>\n<p><a href="https://social.msdn.microsoft.com/Forums/en-US/f4a4f73b-3e13-40bf-99df-9c1bba8fe44e/include-existing-folder-path-as-project-item?forum=vsx" rel="nofollow noreferrer">https://social.msdn.microsoft.com/Forums/en-US/f4a4f73b-3e13-40bf-99df-9c1bba8fe44e/include-existing-folder-path-as-project-item?forum=vsx</a></p>\n<p>If the folder does not physically exist, you can use <code>AddFolder(folderName)</code>. But if the folder is not included in the project while existing physically, you need to provide the full system path to the folder. (<code>AddFolder(fullPath)</code>)</p>\n'}]
['https://Stackoverflow.com/questions/64388', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/8586/']