Add custom file properties programmatically [duplicate] - c#

This question already has answers here:
Add new metadata properties to a file
(1 answer)
Custom File Properties
(4 answers)
Closed 4 years ago.
I need to add custom file properties (see here) to thousands of files programmatically.
The WindowsAPICodePack can get/set existing file properties, but it seems it can not add custom properties?!?
Here the code that works based on Add new metadata properties to a file:
You must reference the DSOFile.dll which can be downloaded from Microsoft here:
Microsoft Developer Support OLE File Property Reader 2.1 Sample
using DSOFile;
OleDocumentProperties myFile = new DSOFile.OleDocumentProperties();
myFile.Open(#"c:\temp\B30700.asm", false, DSOFile.dsoFileOpenOptions.dsoOptionDefault);
bool property_exists;
object prop_value;
prop_value = "999";
//Then check if there's already a property like the one you want to create
property_exists = false;
foreach (DSOFile.CustomProperty property in myFile.CustomProperties)
{
if (property.Name == "Your Property Name")
{
//Property exists
//End the task here (return;) oder edit the property
property_exists = true;
property.set_Value(prop_value);
}
}
if (!property_exists)
myFile.CustomProperties.Add("Your Property Name", ref prop_value);
myFile.Save();
myFile.Close(true);

Related

How to create a file relative to Program.cs [duplicate]

This question already has answers here:
Get the application's path
(21 answers)
Closed 3 months ago.
The community reviewed whether to reopen this question 3 months ago and left it closed:
Original close reason(s) were not resolved
I want to create a file relative to Program.cs. This code in case of VSCode works correct:
string myFile1 = #".\temp1.txt";
File.Create(myFile1);
string myFile2 = "./temp2.txt";
File.Create(myFile2);
but Visual Studio IDE 2022 creates file in:
`MyProject\bin\Debug\net6.0`
Is there any universal solution?
You can include the following method anywhere within your project:
using System.Runtime.CompilerServices
public static string GetSourceFilePathName( [CallerFilePath] string? callerFilePath = null )
=> callerFilePath ?? "";
Then, you can invoke that method from your Program.cs, and it will give you C:\Users\YOU\Documents\Projects\MyProject\Program.cs.
I suppose you know what to do from there.

Property of an AppModel application not getting updated in SCCM

I'm trying to update SDMPackageXML property of an AppModel application through C# code. SDMPackageXML is an XML property. I've to update only one node named AutoInstall in the
SDMPackageXML XML property. Here is my code:
ObjectGetOptions opt = new ObjectGetOptions(null, System.TimeSpan.MaxValue, true);
var path = new ManagementPath("SMS_Application.CI_ID=16777568");
ManagementObject obj = new ManagementObject(scope, path, opt);
obj.Get();
foreach (PropertyData property in obj.Properties)
{
if (property.Name == "SDMPackageXML")
{
//change the property value. Set AutoInstall to true
XmlDocument xml = new XmlDocument();
xml.LoadXml(property.Value.ToString());
var autoInstallTag = xml.GetElementsByTagName("AutoInstall");
autoInstallTag[0].InnerText = "false";
property.Value = xml.OuterXml;
}
}
obj.Put();
The problem is that obj.Put(); updates nothing on the SCCM server. Can someone help me please?
So similar to what I talked about in this answer the main problem here is that Microsoft uses a special method to serialize their XML. The deserialization still works with using the default classes but to serialize again there is no documentation as to how to (I'm pretty sure it is possible but I am not knowledgeable enough to do it)
Instead of documentation they provide wrapper classes for this which are shipped with the SCCM Console (Located in the bin directory of the Installation folder of the Console).
In this case this would be Microsoft.ConfigurationManagement.ApplicationManagement.dll. Unlike in powershell where the dependencies in the same path seem to be loaded as well you seem also to have to reference at least Microsoft.ConfigurationManagement.ApplicationManagement.TaskSequenceInstaller.dll as well.
There are also further dlls with names like Microsoft.ConfigurationManagement.ApplicationManagement.MsiInstaller.dll present however at least in my tests the two above were the only ones needed, but if you notice the deserialization failing with "InvalidPropertyException" errors you might need the dll matching your specific application type.
With those two dlls referenced you can write something like this (note I deserialized using the dll as well because why not if it is already loaded and it creates a nice application object to directly modify the properties. This is however technically not necessary. You could deserialize like in your example and only use the serialization part.
ManagementObject obj = new ManagementObject(#"\\<siteserver>\root\SMS\site_<sitecode>:SMS_Application.CI_ID=<id>");
Microsoft.ConfigurationManagement.ApplicationManagement.Application app = Microsoft.ConfigurationManagement.ApplicationManagement.Serialization.SccmSerializer.DeserializeFromString(obj["SDMPackageXML"].ToString(), true);
app.AutoInstall = true;
obj["SDMPackageXML"] = Microsoft.ConfigurationManagement.ApplicationManagement.Serialization.SccmSerializer.SerializeToString(app, true);
obj.Put();
Now one thing to keep in mind is that is can be a little tricky referencing the applications by their CI_ID because if you update the application the id for the currently valid version of the app changes (the old id still can be used to reference the older revision). So if you change the application gotten using the ID and then change it back with the same ID it will look like only the first change worked. I don't know if this is problematic for you (If you just get all IDs then change every application only once it should not matter) but if it does you can search for the application using their name plus isLatest = 'true' in the WQL query to always get the current one.

How to use EntityFramework BulkInsert? [duplicate]

This question already has an answer here:
How to use EntityFramework.BulkInsert?
(1 answer)
Closed 8 years ago.
I'm trying to use, this, but the system can not find the methods of lib...
Nothing that is specified in the documentation of the lib. is running, for example: GetContext() is not found, the own BulkInser is not found .... I put at the top of my code the Using of lib, but nothing works .....
How I can make to use that Lib ? ( I'm using the VS2013 )
My code:
using EntityFramework.BulkInsert.Extensions;
using (var transactionScope = new TransactionScope())
{
var ctx = new MyDBCon.MyDBDataContext();
ctx.BulkInsert(linhas); // error in BulkInsert ( method not found )
ctx.SubmitChanges();
transactionScope.Complete();
}
You need to include the library...
using EntityFramework.BulkInsert.Extensions;
Add this to the top of your class.

Where are the default WPF Control Templates? [duplicate]

This question already has answers here:
How to Extract Default Control Template In Visual Studio?
(6 answers)
Closed 5 years ago.
The default Control Templates exist, obviously, in binary form in the different DLLs that we add as "References" to each of our projects.
Do they exist in ASCII/source form somewhere? Are they available?
TIA
There are free tools available to explore the templates of the default wpf controls, e.g. Show Me The Template! (there are also tools for control styles, see StyleSnooper).
Alternatively you can explore templates with the following:
private string getTemplate(Control control)
{
StringBuilder stringBuilder = new StringBuilder();
XmlWriterSettings xmlSettings = new XmlWriterSettings();
xmlSettings.Indent = true;
using (XmlWriter xmlWriter = XmlWriter.Create(stringBuilder, xmlSettings))
{
XamlWriter.Save(control.Template, xmlWriter);
}
return stringBuilder.ToString();
}
They are freely available on MSDN. The "directory" page can be found at:
http://msdn.microsoft.com/en-us/library/aa970773(v=vs.110).aspx
I realize this is a link-only answer, but the answer really is a link...
Currently, googling "WPF Control templates" shows the linked article as the second result. If the link changes, the same search may yield good results.

Getting the selected file and using IVsSingleFileGenerator

I am trying to add several new functions to a package I developed for our company. One of these functions is to create a new file based on the file selected in the solution explorer and the menu option selected. I have created my dynamic menu items that are on the solution explorer and it looks like I need to use IVsSingleFileGenerator as shown in this sample.
The trouble I am having is getting the file I have selected and either reading it or passing it into a single file generator.
I would rather generate the file from this context menu than from a custom tool action
Looks like this will get me the path of the file
UIHierarchy solutionExplorer = _applicationObject.ToolWindows.SolutionExplorer;
var items = solutionExplorer.SelectedItems as Array;
if (items == null || items.Length != 2)
{
return;
}
String strFile1 = String.Empty;
UIHierarchyItem item1 = items.GetValue(0) as UIHierarchyItem;
foreach (UIHierarchyItem hierarchyItem in items)
{
ProjectItem prjItem = hierarchyItem.Object as ProjectItem;
string prjPath = prjItem.Properties.Item("FullPath").Value.ToString();
}
which I can then use to pass into the generate function of the ivs single file generator.
Is this the best approach?
I also posted this question to the msdn forums at:
VSX getting the selected file and using IVsSingleFileGenerator
I ended up using the IPyIntegration sample to figure out what i needed to do.

Categories

Resources