private void EditWMIQuery_Shown(object sender, EventArgs e) { Cursor.Current = Cursors.WaitCursor; txtMachine.Text = MachineName; cboNamespace.Text = RootNameSpace; txtQuery.Text = QueryText; LoadNameSpaces(); LoadClasses(); CheckOKEnabled(); try { loading = true; WMIQueryParser wmiQueryParser = new WMIQueryParser(); wmiQueryParser.QueryText = txtQuery.Text; if (wmiQueryParser.IsParsed) { cboClass.Text = wmiQueryParser.TableName; for (int i = 0; i < lstProperties.Items.Count; i++) { if (wmiQueryParser.Fields.Contains(lstProperties.Items[i].ToString())) { lstProperties.SelectedIndices.Add(i); } } } } catch { } loading = false; Cursor.Current = Cursors.Default; }
private void GenerateQuery() { string machineName = ApplyConfigVarsOnField(txtMachine.Text); if (machineName != null && machineName.Length > 0 && cboNamespace.Text != null && cboNamespace.Text.Length > 0 && cboClass.Text != null && cboClass.Text.Length > 0) { string oldTable = ""; string oldWhere = ""; //First check if Table stayed the same to try and preserve WHERE clause try { WMIQueryParser wmiQueryParser = new WMIQueryParser(); wmiQueryParser.QueryText = txtQuery.Text; if (wmiQueryParser.IsParsed) { oldTable = wmiQueryParser.TableName; oldWhere = wmiQueryParser.WhereText; } } catch { } StringBuilder queryText = new StringBuilder(); queryText.Append("select "); StringBuilder propertyList = new StringBuilder(); if (lstProperties.SelectedIndices == null || lstProperties.SelectedIndices.Count == 0) { propertyList.Append("*"); } else { foreach (string p in lstProperties.SelectedItems) { propertyList.Append(p + ","); } } queryText.Append(propertyList.ToString().TrimEnd(',')); queryText.Append(" from " + cboClass.Text); txtQuery.Text = queryText.ToString(); if (oldWhere.Length > 0 && oldTable.ToLower() == cboClass.Text.ToLower()) { txtQuery.Text += " where " + oldWhere; } CheckOKEnabled(); } }
private void cmdTest_Click(object sender, EventArgs e) { try { Cursor.Current = Cursors.WaitCursor; WMIQueryParser wmiQueryParser = new WMIQueryParser(); wmiQueryParser.QueryText = txtQuery.Text; if (wmiQueryParser.IsParsed) { wmiQueryParser.Machines.Add(txtMachine.Text); wmiQueryParser.Namespace = cboNamespace.Text; DataSet queryData = wmiQueryParser.RunQuery(); StringBuilder sb = new StringBuilder(); if (queryData != null && queryData.Tables.Count > 0) { bool first = true; int colCount = queryData.Tables[0].Columns.Count; foreach (DataColumn col in queryData.Tables[0].Columns) { sb.Append((first ? "" : ",") + "\"" + col.ColumnName + "\""); first = false; } sb.AppendLine(); foreach (DataRow r in queryData.Tables[0].Rows) { first = true; for (int i = 0; i < colCount; i++) { if (r[i].GetType().IsArray) { string outValue = ""; if (r[i].GetType().ToString().ToLower().Contains("uint16")) { UInt16[] intArr = (UInt16[])r[i]; for (int ii = 0; ii < intArr.Length; ii++) { outValue += intArr[ii].ToString() + ";"; } } else { string[] theArr = (string[])r[i]; for (int ii = 0; ii < theArr.Length; ii++) { outValue += theArr[ii].ToString() + ";"; } } outValue = outValue.Trim(';'); sb.Append((first ? "" : ",") + "\"" + outValue + "\""); } else { sb.Append((first ? "" : ",") + "\"" + r[i].ToString() + "\""); } first = false; } sb.AppendLine(); } } string outputFileName = System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "QMWMIQuery.csv"); System.IO.File.WriteAllText(outputFileName, sb.ToString()); System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo = new System.Diagnostics.ProcessStartInfo(); p.StartInfo.FileName = outputFileName; p.Start(); } else { MessageBox.Show("Query could not be parsed!\r\nCheck query syntax", "Parse", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } Cursor.Current = Cursors.Default; }