Visual Studio 扩展性:将现有文件夹添加到项目中

发布于 2024-07-05 07:45:17 字数 746 浏览 7 评论 0原文

我正在尝试使用 Visual Studio 2008 的可扩展性来编写一个外接程序,该外接程序将在解析接口后创建一个包含各种消息的项目文件夹。 但是,我在创建/添加文件夹的步骤中遇到了麻烦。 我尝试使用

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

....也许答案在于删除后以编程方式刷新项目? 这怎么办呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

寄风 2024-07-12 07:45:17
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会将目录及其下的所有内容添加到项目中。

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.

贵在坚持 2024-07-12 07:45:17

是的,就是这样...

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

如果有一种更优雅的方式来做到这一点,我们将不胜感激...

谢谢。

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.

太阳哥哥 2024-07-12 07:45:17

这是我的方法:

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

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");
紫罗兰の梦幻 2024-07-12 07:45:17

我正在开发 Visual Studio 2019 的扩展,并且遇到了类似的问题。 下一页中提出的问题帮助了我:

https://social.msdn.microsoft.com/Forums/en-US/f4a4f73b-3e13-40bf-99df-9c1bba8fe44e/include-existing -folder-path-as-project-item?forum=vsx

如果该文件夹实际不存在,您可以使用 AddFolder(folderName)。 但是,如果该文件夹在物理存在时未包含在项目中,则需要提供该文件夹的完整系统路径。 (添加文件夹(完整路径)

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

紧拥背影 2024-07-12 07:45:17

这是我想到的一个想法,因为我已经使用 NAnt 很长时间了,并且认为它可能有效。

在文本编辑器中打开 .csproj 文件并添加目录,如下所示:

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

如果“ItemGroup”已存在,那就没问题。 只需将其添加到现有的中即可。 Visual Studio 并不真正知道如何编辑此条目,但它会扫描整个目录。

编辑成你想要的任何内容。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文