gpt4 book ai didi

c# - IVsProject.AddItem 的用法示例

转载 作者:太空宇宙 更新时间:2023-11-03 11:21:37 24 4
gpt4 key购买 nike

任何人都有 IVsProject.AddItem 的示例,直到现在我已经完成了以下操作,但不明白如何使用 IVsProject.AddItemmsdn没有任何例子。

private void MenuItemCallback(object sender, EventArgs e)
{
IVsSolution solutionService = GetService(typeof(SVsSolution)) as IVsSolution;
// get all projects in solution
IEnumHierarchies enumHierarchies = null;
Guid guid = Guid.Empty;
ErrorHandler.ThrowOnFailure(solutionService.GetProjectEnum(
(uint)__VSENUMPROJFLAGS.EPF_ALLINSOLUTION,
ref guid,
out enumHierarchies));
//Loop all projects found
if (enumHierarchies != null)
{
// Loop projects found
IVsHierarchy[] hierarchy = new IVsHierarchy[1];
uint fetched = 0;

while (enumHierarchies.Next(1, hierarchy, out fetched) == VSConstants.S_OK
&& fetched == 1)
{
Guid projectGuid;
ErrorHandler.ThrowOnFailure(hierarchy[0].GetGuidProperty(
VSConstants.VSITEMID_ROOT,
(int)__VSHPROPID.VSHPROPID_ProjectIDGuid,
out projectGuid));
IVsProject project = (IVsProject)hierarchy[0];
project.AddItem(VSConstants.VSITEMID_ROOT,
VSADDITEMOPERATION.VSADDITEMOP_OPENFILE,
1,
...)

}
}
}

以下使用 DTE 也不起作用

private void MenuItemCallback(object sender, EventArgs e)
{
//GemFireWizardForm form = new GemFireWizardForm();
//form.ShowDialog();
//Get the solution service

IVsSolution solutionService = GetService(typeof(SVsSolution)) as IVsSolution;
// get all projects in solution
IEnumHierarchies enumHierarchies = null;
Guid guid = Guid.Empty;
ErrorHandler.ThrowOnFailure(solutionService.GetProjectEnum(
(uint)__VSENUMPROJFLAGS.EPF_ALLINSOLUTION,
ref guid,
out enumHierarchies));
//Loop all projects found
if (enumHierarchies != null)
{
// Loop projects found
IVsHierarchy[] hierarchy = new IVsHierarchy[1];
uint fetched = 0;
Guid projectGuid = Guid.Empty;
while (enumHierarchies.Next(1, hierarchy, out fetched) == VSConstants.S_OK
&& fetched == 1)
{
ErrorHandler.ThrowOnFailure(hierarchy[0].GetGuidProperty(
VSConstants.VSITEMID_ROOT,
(int)__VSHPROPID.VSHPROPID_ProjectIDGuid,
out projectGuid));
}

IVsHierarchy ppHierarchy = null;
IVsSolution solution = (IVsSolution)GetService(typeof(SVsSolution));
Int32 result = solution.GetProjectOfGuid(ref projectGuid, out ppHierarchy);

if (ppHierarchy != null && result == VSConstants.S_OK)
{
Object automationObject = null;
ppHierarchy.GetProperty(VSConstants.VSITEMID_ROOT,
(Int32)__VSHPROPID.VSHPROPID_ExtObject,
out automationObject);

if (automationObject != null)
{
EnvDTE.SolutionClass sc = automationObject as EnvDTE.SolutionClass;
EnvDTE.Projects projects = sc.Projects;
EnvDTE.Project project = projects.Item(1);
EnvDTE.ProjectItems pitems = project.ProjectItems;

pitems.AddFromFileCopy(@"e:\avinash\test.cpp");
}
}
}
}

scnull

的形式出现

最佳答案

我使用这段代码设法将文件添加到当前项目的根目录中:

string file = ... ; // Provide the absolute path of your file here
string[] files = new string[1] { file };
VSADDRESULT[] result = new VSADDRESULT[1];
proj.AddItem(
VSConstants.VSITEMID_ROOT,
VSADDITEMOPERATION.VSADDITEMOP_OPENFILE,
file,
1,
files,
IntPtr.Zero,
result);

不幸的是,使用 IVsProject.AddItem 似乎无法向项目添加过滤器,或在过滤器下添加文件。事实上,MSDN documentation提到参数 itemidloc :

itemidLoc
Type: System.UInt32
[in] Identifier of the container folder for the item being added. Should be VSITEMID_ROOT or other valid item identifier. See the enumeration VSITEMID. Note that this parameter is currently ignored because only adding items as children of a project node is supported. Projects that support the notion of folders will want to add the items relative to itemidLoc.

关于c# - IVsProject.AddItem 的用法示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10779974/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com