將屬性加入至專案項目
如需 Visual Studio 2017 的最新文件請參閱 Visual Studio 2017 文件。
方法GetItemAttribute和SetItemAttribute取得並設定專案項目之屬性的值。 SetItemAttribute 建立屬性如果已經存在,將它加入專案項目中繼資料。
將屬性加入至專案項目
若要將屬性加入至專案項目
下列程式碼會使用DTE automation 物件和SetItemAttribute方法,將屬性加入至專案項目。 專案項目 ID 被取自專案項目名稱"program.cs 」。 屬性"MyAttribute"會加入至這個專案項目,並給定值 「 MyValue 」。
EnvDTE.DTE dte = (EnvDTE.DTE)Package.GetGlobalService(typeof(EnvDTE.DTE)); EnvDTE.Project project = dte.Solution.Projects.Item(1); string uniqueName = project.UniqueName; IVsSolution solution = (IVsSolution)Package.GetGlobalService(typeof(SVsSolution)); IVsHierarchy hierarchy; solution.GetProjectOfUniqueName(uniqueName, out hierarchy); IVsBuildPropertyStorage buildPropertyStorage = hierarchy as IVsBuildPropertyStorage; if (buildPropertyStorage != null) { uint itemId; string fullPath = (string)project.ProjectItems.Item("Program.cs").Properties.Item("FullPath").Value; hierarchy.ParseCanonicalName(fullPath, out itemId); buildPropertyStorage.SetItemAttribute( itemId, "MyAttribute", "MyValue"); }