2008-09-15 153 views
5

我试图使用Visual Studio 2008的可扩展性来编写一个插件,该插件将在解析接口后创建一个包含各种消息的项目文件夹。不过,我在创建/添加文件夹的步骤中遇到了麻烦。我已经使用Visual Studio Extensibility:将现有文件夹添加到项目

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

尝试(项目是我的目标文件的旁边,我正在创建具有相同名称的文件夹,但“消息”追加到它),但是当一个文件夹已存在,扼流圈(无大惊喜)。

我试着删除它,如果它已经存在,如:

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

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

我可以看到该文件夹​​被在调试时被删除,但它仍然 似乎认为该文件夹仍然存在,并且死的已有一个文件夹 存在异常。

任何想法???

谢谢。

AK

....也许答案就在于编程方式刷新后删除该项目?这可以怎么做?

+0

任何有关它的解决方案吗?任何答案都有帮助? – Kiquenet 2012-10-17 10:16:12

回答

3

是啊,这是它...

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

如果有这样做的更优雅的方式,这将是非常赞赏...

感谢。

0

这里有一个想法,因为我一直在使用NAnt这么长时间,并认为它可能工作。

在文本编辑器中的.csproj文件,并添加目录,例如:

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

如果“的ItemGroup”已经esists,这很好。只需将其添加到现有的一个。 Visual Studio不会真的知道如何编辑这个条目,但它会扫描整个目录。

编辑任何你想要的。

3
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将把该目录和目录下的所有内容添加到项目中。

2

这是我的方法:

//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"); 
相关问题