我正在尝试使用Visual Studio 2008的可扩展性来编写一个插件,该解决方案将在解析接口后创建一个包含各种消息的项目文件夹。但是,我在创建/添加文件夹的步骤中遇到了麻烦。我尝试过使用
ProjectItem folder =
item.ProjectItem.Collection.AddFolder(newDirectoryName, string.Empty);
(项目是我的目标文件,我正在创建一个名称相同但附加了“Messages”的文件夹),但是当文件夹已经存在时会窒息(没什么大不了的)。
我尝试删除它,如果它已经存在,例如:
DirectoryInfo dirInfo = new DirectoryInfo(newDirectoryParent +
newDirectoryName);
if (dirInfo.Exists)
{
dirInfo.Delete(true);
}
ProjectItem folder =
item.ProjectItem.Collection.AddFolder(newDirectoryName, string.Empty);
我可以看到在调试时文件夹被删除了,但它仍然存在 似乎认为文件夹仍然存在并且已经死在文件夹上 存在例外。
任何想法???
感谢。
AK
....也许答案在于删除后以编程方式刷新项目?怎么可能这样做?
答案 0 :(得分: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);
如果有一种更优雅的方式,我们将非常感激......
感谢。
答案 1 :(得分: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 :(得分: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");
答案 3 :(得分:0)
这是我想到的一个想法,因为我一直在使用NAnt并认为它可能有用。
在文本编辑器中打开.csproj文件并添加目录:
<ItemGroup>
<compile include="\path\rootFolderToInclude\**\*.cs" />
</ItemGroup>
如果“ItemGroup”已经存在,那很好。只需将其添加到现有的中即可。 Visual Studio不会真正知道如何编辑这个条目,但它会扫描整个目录。
根据您的喜好编辑。
答案 4 :(得分:0)
我正在为Visual Studio 2019开发一个扩展,并且遇到了类似的问题。下一页中提出的问题帮助了我
如果该文件夹实际上不存在,则可以使用AddFolder(folderName)
。但是,如果该文件夹实际上不存在于项目中,则需要提供该文件夹的完整系统路径。 (AddFolder(fullPath)
)