-
I am using the toolkit and I add a file to a project. After this I want to change the Build Action Property from None to Resource. I have first tried with the TrySetAttributeAsync method but it fails. I have tried by using the SetProperty method of the IVsHierarchy but it fails. PhysicalFile menuXml ... code to create the PhysicalFile r = -2147352573 Any idea to change the Build Action? Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
@reduckted any idea? |
Beta Was this translation helpful? Give feedback.
-
I found this answer on Stack Overflow which gives a solution that works (with a few modifications): https://stackoverflow.com/a/24538728/4397397 ℹ️ See my comment below for an even simpler solution. Here's a simplified solution: private void SetBuildAction(PhysicalFile file, string buildAction)
{
ThreadHelper.ThrowIfNotOnUIThread();
file.GetItemInfo(out IVsHierarchy hierarchy, out uint itemId, out _);
int result = hierarchy.SetProperty(itemId, (int)__VSHPROPID4.VSHPROPID_BuildAction, buildAction);
if (ErrorHandler.Succeeded(result))
{
return;
}
result = hierarchy.GetProperty(itemId, (int)__VSHPROPID.VSHPROPID_BrowseObject, out object browseObject);
if (ErrorHandler.Succeeded(result))
{
PropertyDescriptorCollection propertyDescriptors = TypeDescriptor.GetProperties(browseObject);
if (propertyDescriptors != null)
{
PropertyDescriptor itemTypeDescriptor = GetPropertyDescriptor(propertyDescriptors, "ItemType");
if (itemTypeDescriptor != null)
{
try
{
itemTypeDescriptor.SetValue(
browseObject,
itemTypeDescriptor.Converter.ConvertFromInvariantString(buildAction)
);
return;
}
catch (NotSupportedException)
{
// Suppress this error. We'll throw our
// own `NotSupportedException` further below.
}
}
}
}
throw new NotSupportedException($"Could not set the build action of '${file.FullPath}'.");
}
private static PropertyDescriptor GetPropertyDescriptor(PropertyDescriptorCollection properties, string name)
{
// The property names seem to be wrapped in {}, so try to find the given
// name first, and if that doesn't work, then wrap the name in braces.
return properties[name] ?? properties[$"{{{name}}}"];
} |
Beta Was this translation helpful? Give feedback.
I found this answer on Stack Overflow which gives a solution that works (with a few modifications): https://stackoverflow.com/a/24538728/4397397
ℹ️ See my comment below for an even simpler solution.
Here's a simplified solution: