Determine Windows Update classification - c#

From the Windows Update COM Library (WUAPILib) I have access to the IUpdate interface however I don't see of any way to use to get the update classification (Critical, Important, Optional) to group updates in the same way like the Windows Update UI in Control Panel does.

With the help of the IUpdate, you can get the IcategoryCollection from the Update ID.
Now, the first ICategory stores the classification of update type for the OS. Do pay special attention to the line where comment is placed:
Console.WriteLine("Patch name = " + ic.Name.ToString());
// In the ICategory collection, first element ICategory stores information of "Update Classification";
// whereas second Icategory element stores the product type information.
Test Code:
UpdateSession uSession = new UpdateSession();
IUpdateSearcher uSearcher = uSession.CreateUpdateSearcher();
uSearcher.Online = false;
ISearchResult sResult = uSearcher.Search("IsInstalled=1 And IsHidden=0");
Console.WriteLine("Found " + sResult.Updates.Count + " updates" + Environment.NewLine);
foreach (IUpdate update in sResult.Updates)
{
Console.WriteLine();
Console.WriteLine("Required update " + update.KBArticleIDs[0].ToString() + " is installed...");
Console.WriteLine("Update ID = "+update.Identity.UpdateID);
ICategoryCollection icc = update.Categories;
foreach (ICategory ic in icc)
{
Console.WriteLine("Patch description = " + ic.Description.ToString());
Console.WriteLine("Patch category = " + ic.CategoryID.ToString());
Console.WriteLine("Patch Type = " + ic.Type.ToString());
Console.WriteLine("Patch name = " + ic.Name.ToString());
// only first ICategory element stores the patch name,
// which reveals the Classification information
}
}
Sample Output:

Related

changing two dropdownlists selected item from sql table but both get same selected value

I have two dropdowns initializing from same sql table:
ddlETCsc1.Items.Clear();
ddlETCsc2.Items.Clear();
foreach (var PSiteContacts in ContactsAdapter.GetPSiteContacts(Cus_Id))
{
var item = new System.Web.UI.WebControls.ListItem();
item.Text = PSiteContacts.name + " / " + PSiteContacts.phone;
item.Value = PSiteContacts.name + " / " + PSiteContacts.phone;
item.Attributes.Add("data-subtext", PSiteContacts.con_type);
ddlETCsc1.Items.Add(item);
ddlETCsc2.Items.Add(item);
}
ddlETCsc1.Items.Insert(0, new System.Web.UI.WebControls.ListItem("Nothing Selected", "0"));
ddlETCsc2.Items.Insert(0, new System.Web.UI.WebControls.ListItem("Nothing Selected", ""));
I am changing there selected item from sql table columns SContact1 and Scontact2 these have different data but both get same selected item:
ddlETCsc1.SelectedValue = reader["SContact1"].ToString();
ddlETCsc2.SelectedValue = reader["SContact2"].ToString();
I can see by addiong breakpoints that ddlETCsc1 gets correct value first but when ddlETCsc2 value changes the ddlETCsc1 get same value as ddlETcsc2.
It works fine for first time(i have dropdowns in bootstrap modal) but when i adds new contact(button on modal that opens another modal to add contact and after adding opens previous modal and also initialize the dropdowns again with new values and fetch values from sql) then the selected value not work as expected
Any help will be appreciated. Thanks in advance
The reason is because when you add item from your ContactsAdapter iteration, you refer to the same item (same ListItem) reference for each item in ddlETCsc1 and ddlETCsc2 (except for your "0" and "" later on). Try to change your code to:
ddlETCsc1.Items.Clear();
ddlETCsc2.Items.Clear();
foreach (var PSiteContacts in ContactsAdapter.GetPSiteContacts(Cus_Id))
{
var item1 = new System.Web.UI.WebControls.ListItem();
item1.Text = PSiteContacts.name + " / " + PSiteContacts.phone;
item1.Value = PSiteContacts.name + " / " + PSiteContacts.phone;
item1.Attributes.Add("data-subtext", PSiteContacts.con_type);
ddlETCsc1.Items.Add(item1);
var item2 = new System.Web.UI.WebControls.ListItem();
item2.Text = PSiteContacts.name + " / " + PSiteContacts.phone;
item2.Value = PSiteContacts.name + " / " + PSiteContacts.phone;
item2.Attributes.Add("data-subtext", PSiteContacts.con_type);
ddlETCsc2.Items.Add(item2);
}
ddlETCsc1.Items.Insert(0, new System.Web.UI.WebControls.ListItem("Nothing Selected", "0"));
ddlETCsc2.Items.Insert(0, new System.Web.UI.WebControls.ListItem("Nothing Selected", ""));
To create two different references (albeit having the same initial value) for each item inserted in the ddlETCsc1 and ddlETCsc2

c# List inside list XML Linq

I have the following statement
xdoc.Descendants("Father").Select(p => new
{
Son1 = (string)p.Element("Son1").Value,
Son2 = (string)p.Element("Son2").Value,
Son3= (string)p.Element("Son3").Value,
Son4 = (string)p.Element("Son4").Value,
Son5 = (string)p.Element("Son5").Value
}).ToList().ForEach(p =>
{
Response.Write("Son1= " + p.Son1 + " ");
Response.Write("Son2=" + p.Son2 + " ");
Response.Write("Son3=" + p.Son3 + " ");
Response.Write(("Son4 =") + p.Son4 + " ");
Response.Write(("Son5 =") + p.Son5 + " ");
Response.Write("<br />");
});
and it works fine as long as i have only one instance of each son , the problem is that i have multiple instances of Son5, and i donĀ“t know how to put Son5 inside of a list
Here is my XML code Example:
If you have several elements of same type, then you should parse them to list or other collection:
var fathers = from f in xdoc.Descendants("Father")
select new {
Son1 = (string)f.Element("Son1"),
Son2 = (string)f.Element("Son2"),
Son3= (string)f.Element("Son3"),
Son4 = (string)f.Element("Son4"),
Son5 = f.Elements("Son5").Select(s5 => (string)s5).ToList()
};
Some notes:
Don't use .Value of XElement or XAttribute - you can cast element itself to appropriate data type without accessing its value. Benefits - less code, more reliable in case element is missing (you will not get NullReferenceException)
Consider to use int or int? as elemenent values if your elements contain integer values
If you have single Father element, then don't work with collection of fathers. Just get xml root and check whether it's null or not. After that you can create single father object.
Writing response
foreach(var father in fathers)
{
Response.Write($"Son1={father.Son1} ");
Response.Write($"Son2={father.Son2} ");
Response.Write($"Son3={father.Son3} ");
Response.Write($"Son4={father.Son4} ");
Response.Write(String.Join(" ", father.Son5.Select(son5 => $"Son5={son5}"));
Response.Write("<br />");
}
Try this:
xdoc.Descendants("Father").Select(p => new
{
Son1 = p.Element("Son1").Value,
Son2 = p.Element("Son2").Value,
Son3= p.Element("Son3").Value,
Son4 = p.Element("Son4").Value,
Sons5 = p.Elements("Son5").Select(element => element.Value).ToList()
}).ToList().ForEach(p =>
{
Response.Write("Son1= " + p.Son1 + " ");
Response.Write("Son2=" + p.Son2 + " ");
Response.Write("Son3=" + p.Son3 + " ");
Response.Write("Son4 =" + p.Son4 + " ");
p.Sons5.ForEach(son5 => Response.Write("Son5 =" + son5 + " "));
Response.Write("<br />");
});
That will create a list of Son5 within your list of items, which you can iterate in the ForEach with another ForEach.

Load a specific row as default item in combobox

I fill combo-box with a data from database Access but my question is how can I put a specific row in database filled as a Default item in combo-box with C#?
gerant remplirlistgerant = new gerant();
foreach (gerant ligne in remplirlistgerant.getinfogerant())
{
cmbgerant.Items.Add(ligne.CIN_GERANT + " - " + ligne.NOM_GERANT + " - " + ligne.PRENOM_GERANT);
}
Hope I know yours,
Example if ligne.CIN_GERANT = "aaa" the current row will be selected.
gerant remplirlistgerant = new gerant();
foreach (gerant ligne in remplirlistgerant.getinfogerant())
{
cmbgerant.Items.Add(ligne.CIN_GERANT + " - " + ligne.NOM_GERANT + " - " + ligne.PRENOM_GERANT);
// Example if ligne.CIN_GERANT = "aaa" then select this row.
if (ligne.CIN_GERANT == "aaa" )
{
cmbgerant.SelectedIndex = cmbgerant.Items.Count - 1;// Item just added
}
}

Loop LDAP memberOf as return to show all for single user

Pretty new to using LDAP, and C# in general, and I did more than a few searches, but most of my attempted fixes have lead nowhere.
I am pulling information from the LDAP. Everything works, except I can only pull the memberOf information if I am explicit in which array number I want. Attempts to use a foreach, or a for statement have lead nowhere. I know I am probably missing something simple, but I figured I should just ask.
public static String FindOther(String userAccount)
{
DirectoryEntry entry = GetDirectoryEntry();
DirectorySearcher search = new DirectorySearcher(entry);
try
{
search.Filter = "(SAMAccountName=" + account + ")";
search.PropertiesToLoad.Add("distinguishedName");
search.PropertiesToLoad.Add("displayName");
search.PropertiesToLoad.Add("mail");
search.PropertiesToLoad.Add("CN");
search.PropertiesToLoad.Add("Title");
search.PropertiesToLoad.Add("sn");
search.PropertiesToLoad.Add("givenname");
search.PropertiesToLoad.Add("telephoneNumber");
search.PropertiesToLoad.Add("memberOf");
SearchResult result = search.FindOne();
if (result != null)
{
return
"Results for " + userAccount + "\n" +
" DistinguishedName..: " + result.Properties["distinguishedName"][0].ToString() + "\n" +
" Displayname........: " + result.Properties["displayname"][0].ToString() + "\n" +
" eMail..............: " + result.Properties["mail"][0].ToString() + "\n" +
" Common Name........: " + result.Properties["CN"][0].ToString() + "\n" +
" Title..............: " + result.Properties["Title"][0].ToString() + "\n" +
" Last Name..........: " + result.Properties["sn"][0].ToString() + "\n" +
" First Name.........: " + result.Properties["givenname"][0].ToString() + "\n" +
" Telephone..........: " + result.Properties["telephoneNumber"][0].ToString() + "\n" +
" Member Of..........: " + result.Properties["memberOf"][0].ToString() + "\n" +
" Member Of..........: " + result.Properties["memberOf"][1].ToString() + "\n" +
"End Transmission" + "\n";
}
else
{
return "Object not found... User ID: " + account;
}
}
catch (Exception ex)
{
return "Big Ol Error: " + ex.Message + " User ID: " + account;
}
}
Thank you all for any help you could provide.
You can enumerate through an PropertyCollection this way:
string Ret = string.Empty;
...
foreach(object memberOf in result.Properties["memberOf"])
{
Ret += " Member Of..........: " + memberOf.ToString() + "\n";
}
I'm going to give a slight disclaimer here, mostly because I've never coded to Active Directory or the Lightweight Directory Access Protocol. Some of the things that I do know, is a DirectoryEntry usage recommends:
Use GetDirectoryEntry when you want to look at the live entry instead
of the entry that was returned through DirectorySearcher, or when you
want to invoke a method on the object that was returned.
This particular method will return the information directly from Active Directory. Where DirectorySearcher will only generate through what is currently available in the collection. I mention this, because without the collection being filled it won't generate much.
I'm not sure what type of application your building, but Microsoft has an entire area within Microsoft Developer Network that mentions how to integrate several LDAP / AD features into an application.
I'm not sure of your entire goal, but I believe this is what you are seeking. If not let me know and I'll modify the code.
static void Main(string[] args)
{
string groupName = "Domain Users";
string domainName = "";
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domainName);
GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, groupName);
if (grp != null)
{
foreach (Principal p in grp.GetMembers(false))
{
Console.WriteLine(p.SamAccountName + " - " + p.DisplayName);
}
grp.Dispose();
ctx.Dispose();
Console.ReadLine();
}
else
{
Console.WriteLine("\nWe did not find that group in that domain, perhaps the group resides in a different domain?");
Console.ReadLine();
}
}
This code was actually from a book, it was used to accomplish such a goal. But as I stated up top, I've never physically done such a task- I'm just hoping to point you in the proper direction.
Another question similar to yours can be found here, which contains good reference and answers your question I believe.

TFS API - How to obtain the parent of a work item

My ultimate goal is to get the parent of one work item at a time recursively, until there are no more parents in the hierarchy. At the moment, there is nothing recursive yet, I am still at the point of optimizing the way I obtain the parent work item. I have thought of a way of doing this involving a query:
public WorkItem GetParentWorkItem(int id)
{
StringBuilder queryString = new StringBuilder("SELECT [System.Id]" +
" FROM WorkItemLinks " +
" WHERE [Source].[System.WorkItemType] = '" + TFS_TIMESHEET_WORK_ITEM_TYPE + "'" +
" AND [Source].[System.TeamProject] = '" + TFS_TIMESHEET_PROJECT_KEY + "'" +
" AND [Source].[System.Id] = " + id
);
Query wiQuery = new Query(GetWorkItemStore, queryString.ToString());
WorkItemLinkInfo[] wiTrees = wiQuery.RunLinkQuery();
WorkItem wi = GetWorkItemStore.GetWorkItem(wiTrees[1].TargetId);
return wi;
}
The problem with this method is that it gets all the linked work items, including predecessor, successor, child and parents. I knew that wiTrees[1] was the parent work item so I hard coded the index.
I found out a way to get the "parent" WorkItemTypeEnd object from the work item store:
WorkItemLinkTypeEnd linkTypEnd = GetWorkItemStore.WorkItemLinkTypes.LinkTypeEnds["Parent"];
Where do I go from here?
This works on TFS 2013:
var parent_link = work_item.WorkItemLinks.Cast<WorkItemLink> ().FirstOrDefault (x => x.LinkTypeEnd.Name == "Parent");
WorkItem parent_work_item = null;
if (parent_link != null)
parent_work_item = work_item_store.GetWorkItem (parent_link.TargetId);
Found a solution, which returns the parent WorkItem if there is a parent, if not returns null.
public WorkItem GetParentWorkItem(int id)
{
StringBuilder queryString = new StringBuilder("SELECT [System.Id]" +
" FROM WorkItemLinks " +
" WHERE [Source].[System.WorkItemType] = '" + TFS_TIMESHEET_WORK_ITEM_TYPE + "'" +
" AND [Source].[System.TeamProject] = '" + TFS_TIMESHEET_PROJECT_KEY + "'" +
" AND [Source].[System.Id] = " + id
);
Query wiQuery = new Query(GetWorkItemStore, queryString.ToString());
WorkItemLinkInfo[] wiTrees = wiQuery.RunLinkQuery();
int parentLinkId = GetWorkItemStore.WorkItemLinkTypes.LinkTypeEnds["Parent"].Id;
foreach (WorkItemLinkInfo linkInfo in wiTrees)
{
// -2 is the LinkTypeId for parent
if (linkInfo.LinkTypeId == parentLinkId)
{
workItem = GetWorkItemStore.GetWorkItem(linkInfo.TargetId);
break;
}
else
{
workItem = null;
}
}
return workItem;
}

Categories