Visual Studio Extension - set the property "Copy to output directory"

I am creating a VS extension and I want to add a file to the solution and set some properties. One of them is Copy to output directory , but I can not find a way to configure it. Setting the Build action works fine, but the desired property is not even specified in the array during debugging.

 private EnvDTE.ProjectItem AddFileToSolution(string filePath) { var folder = CurrentProject.ProjectItems.AddFolder(Path.GetDirectoryName(filePath)); var item = folder.ProjectItems.AddFromFileCopy(filePath); item.Properties.Item("BuildAction").Value = "None"; // item.Properties.Item("CopyToOutputDirectory").Value = "CopyAlways"; // doesn't work - the dictionary doesn't contain this item, so it throws an exception return item; } 

How can I set a property for my newly added item?

+5
source share
2 answers

For C # or VB projects, the Cole Wu - MSFT answer should work.

If you are trying to do the same for a project, but you're out of luck, as each type of project has different properties.

From what I tried:

  • C # and VB have 23 properties, including "CopyToOutputDirectory"
  • F # has 9 properties, including "CopyToOutputDirectory"
  • Node.js has 13 properties and the "CopyToOutputDirectory" is missing.

Look at the file properties window in the project you are trying to modify. Does it contain the CopyToOutputDirectory property? If this property may not be available.

EDIT:

Other parameters for setting the ProjectItem property are its attributes (changing them in * .csproj). I would say that this is a workaround, not a real solution, because after that you need to restart the project.

 private EnvDTE.ProjectItem AddFileToSolution(string filePath) { var folder = CurrentProject.ProjectItems.AddFolder(Path.GetDirectoryName(filePath)); var item = folder.ProjectItems.AddFromFileCopy(filePath); item.Properties.Item("BuildAction").Value = "None"; // Setting attribute instead of property, becase property is not available SetProjectItemPropertyAsAttribute(CurrentProject, item, "CopyToOutputDirectory", "Always"); // Reload project return item; } private void SetProjectItemPropertyAsAttribute(Project project, ProjectItem projectItem, string attributeName, string attributeValue) { IVsHierarchy hierarchy; ((IVsSolution)Package.GetGlobalService(typeof(SVsSolution))) .GetProjectOfUniqueName(project.UniqueName, out hierarchy); IVsBuildPropertyStorage buildPropertyStorage = hierarchy as IVsBuildPropertyStorage; if (buildPropertyStorage != null) { string fullPath = (string)projectItem.Properties.Item("FullPath").Value; uint itemId; hierarchy.ParseCanonicalName(fullPath, out itemId); buildPropertyStorage.SetItemAttribute(itemId, attributeName, attributeValue); } } 
+2
source

Please change the code as shown below, it works on my side.

  foreach (Property p in item.Properties) { if (p.Name == "CopyToOutputDirectory") { p.Value = 1; } //dic[p.Name] = p.Value; } 

Update:

Please add the following code to your code and then check if the dictionary object has 'CopyToOutputDirectory'

  Dictionary<string,object> dic = new Dictionary<string, object>(); foreach (Property p in item.Properties) { dic[p.Name] = p.Value; } 
+2
source

Source: https://habr.com/ru/post/1266010/


All Articles