WMI query response is writing to file - c#

I am unable to write the response of a WMI query to file but I can print it to console.
I rewrote the query to use different WMI methods to pull the data. I changed back to the below method of ease of use.
I changed from mo["PackageName"] to mo["PackageName"].ToString() in case the response was not a writable string.
I googled - I have yet to find a similar issue and I am starting to think it is something obvious in my code that I am just overlooking.
//store log in same directory as exe is ran from
StreamWriter writeFile = new StreamWriter(filepath);
ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT PackageName FROM Win32_Product WHERE PackageName LIKE 'jre%%'");
foreach (ManagementObject mo in mos.Get())
{
if (mo["PackageName"].ToString().Contains("jre"))
{
String packageName = mo["PackageName"].ToString();
writeFile.WriteLine(host + "," + packageName);
}
}
update
see my answer below:
foreach (ManagementObject mo in mos.Get())
{
if (mo["PackageName"].ToString().Contains("jre"))
{
String packageName = mo["PackageName"].ToString();
writeFile.WriteLine(host + "," + packageName);
writeFile.Flush();
}
}

Without knowing what writeFile is or how it's defined, I can only suggest that you use the easier System.IO.File class, like so:
File.AppendAllText(pathToYourFile, host + "," + packageName);
This will automatically open, write to, and close your file for you.

I use the WMI Code Creator for WMI query code creation.
It creates C# code and it is possible to test the queries.
Not a real answer - but might help.

I forgot to flush the writer after write.
String packageName = mo["PackageName"].ToString();
writeFile.WriteLine(host + "," + packageName);
writeFile.Flush();

Related

Get VID/ PID of printer using C# with unknown device id

I have a usb printer. I don't know how to take device id also. I want to take vid/ pid using c# code. Any help appreciated.
Have you tried:
using System.Management;
ManagementObjectSearcher myPrinterObject = new ManagementObjectSearcher("select * from Win32_Printer");
foreach (ManagementObject obj in myPrinterObject.Get())
{
Console.WriteLine("Name - " + obj["Name"]);
Console.WriteLine("Network - " + obj["Network"]);
Console.WriteLine("Availability - " + obj["Availability"]);
Console.WriteLine("Is default printer - " + obj["Default"]);
Console.WriteLine("DeviceID - " + obj["DeviceID"]);
Console.WriteLine("Status - " + obj["Status"]);
Console.WriteLine(String.Empty.PadLeft(obj["Name"].ToString().Length, '='));
}
The WMI properties are:
https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-printer?redirectedfrom=MSDN
You can also wrap the manufacturer DLL with a C++ wrapper and then call that from your C# application seamlessly however this will could take you a long time to write!

c# Print PDF with Adobe Reader and close

I didnt found a good (free) and simple Solution for printing PDFs (for e.g. from a "Hot"-Folder (FileSystemWatcher) on a Server) with Acrobat and close Acrobat Reader. So i wrote my own and i hope it will help someone.
(Yes, u can use a old free Foxit Reader Version, but we had too much trouble with it, sometimes stuck in Memory without printing)
The Point was, after printing, the file must be moved to a archive dir, but Adobe did not close. So i never knowed when its done, or wait 30+ Seconds and kill (not so fine if the Server needs longer and takes to much time).
Here my Solution, i run the Process and wait until one of the subprocesses of my Adobe Process shows the recent open Window.
Thanks to mtijn for his "Process Searcher" solution https://stackoverflow.com/a/7189381/480982
var prz = Process.Start("C:\\Program Files (x86)\\Adobe\\Acrobat Reader DC\\Reader\\AcroRd32.exe", "/h /t \"" + YOURPDFFILE + "\" \"" + YOURPRINTER + "\"");
bool loop = true;
while (loop)
{
//u can use Thread.Sleep(x) too;
prz.WaitForExit(500);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(
"SELECT * " +
"FROM Win32_Process " +
"WHERE ParentProcessId=" + prz.Id);
ManagementObjectCollection collection = searcher.Get();
if (collection.Count > 0)
{
foreach (var item in collection)
{
UInt32 childProcessId = (UInt32)item["ProcessId"];
if ((int)childProcessId != Process.GetCurrentProcess().Id)
{
Process childProcess = Process.GetProcessById((int)childProcessId);
//If a File is open the Title begins with "Filename - Adobe ...", but after print/closing the recent window starts with "Adobe Acr..."
if(childProcess.MainWindowTitle.StartsWith("Adobe Acrobat"))
{
loop = false;
break;
}
}
}
}
}
//"Recent" Window found, lets kill the Process
prz.Kill();
// Now here u can move or Delete the pdf file

Unable to get system hard drive SerialNumber using Win32_DiskDrive

I'm using the following code to get my drive serial number. It's working fine with Windows 7, 8, 8.1, and 10 Professional, but I'm getting an error on Windows 10 Home.
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
foreach (ManagementObject wmi_HD in searcher.Get())
{
if (wmi_HD["SerialNumber"] == null)
hddId = null;
else
hddId = wmi_HD["SerialNumber"].ToString();
}
I'm getting
System.NullReferenceException : Object reference not set to an instance of an object.
Does anyone know why? What do I need to do to get the serial number in this case?
One more question: if I boot the OS from my pendrive, will this code work? How could I know that the OS is running from a pendrive or disk or any other resource?
When I go to the Device Manager, I see this:
I am adding this as an answer because it can save lot of time while debugging scenarios like System.NullReferenceException in WMI.
Windows+R (run command)
Type wbemtest
And connect to the machine for which you want to fetch information. Fire the query for Win32_DiskDrive and check the output for properties that you can fetch.
This is what I'm using on Windows 10 v1809:
using System;
using System.Management;
namespace GetSerialNo
{
class Program
{
static void Main(string[] args)
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
foreach (ManagementObject info in searcher.Get())
{
Console.WriteLine("DeviceID: " + info["DeviceID"].ToString());
Console.WriteLine("Model: " + "Model: " + info["Model"].ToString());
Console.WriteLine("Interface: " + "Interface: " + info["InterfaceType"].ToString());
Console.WriteLine("Serial#: " + "Serial#: " + info["SerialNumber"].ToString());
}
Console.ReadLine();
}
}
}
For details please see http://csharphelper.com/blog/2017/10/get-hard-drive-serial-number-c/
For the associated link Get Hard disk serial Number given by #ADreNaLiNe-DJ I wasn't able to find the required assembly reference for HardDrive hd = new HardDrive();

Get driver file version in windows

How can I get file attributes or version info of driver file in windows via c#.
I'm using this code:
var version = File.GetAttributes(Environment.SystemDirectory + #"\drivers\acpi.sys");
but this code throw exception: Could not find file 'C:\Windows\system32\drivers\acpi.sys'..
Then i'm using this code var dir = Directory.GetFiles(Environment.SystemDirectory + #"\drivers"); in dir variable i have 4 files. If I open this folder via windows explorer i have in folder 300+ files. What am I doing wrong?
You should using >= FrameWork .net 4,
add :
using System.Management;
and add the reference System.Management;
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_PnPSignedDriver");
ManagementObjectCollection moc = searcher.Get();
foreach (var manObj in moc)
{
Console.WriteLine("Device Name:" + manObj["FriendlyName"] + " \r\nDeviceID: " + manObj["DeviceID"] + "\r\nDriverDate: " + manObj["DriverDate"] + "\r\nDriverVersion: " + manObj["DriverVersion"] + "\r\nDriverName:" + manObj["DriverName"] +"\n\r======================================\n\n";);
}
Result (part):
Another way could be reading the version from the file directly. Should work for 64/32bit apps, but I am not sure if you can get to all files.
string directory;
if (Environment.Is64BitOperatingSystem)
directory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "Sysnative");
else
directory = Environment.GetFolderPath(Environment.SpecialFolder.System);
var info = FileVersionInfo.GetVersionInfo(Path.Combine(directory, "drivers", "acpi.sys"));
You can find more information about Sysnative folders at Folders under C:\Windows\System32\GroupPolicyUsers created with Directory or File System Redirector

Creating SQL Server backup file (.bak) with c# to any location

I'm trying to write simple application in C# which will allow me to backup, zip and send over ftp my SQL Server database.
One problem I have encountered is that I'm not able to create the backup file (.bak) if I try to do it in different location than "C:\Program Files\Microsoft SQL Server\MSSQL.3\MSSQL\Backup" or "C:\Program Files\Microsoft SQL Server\MSSQL.3\MSSQL\Data" folder. I understand that this is a premission problem. Could someone point me to the resources or write here a short snippet how to programmatically add such a permission to any folder on my system.
Regards
Kris
i assume you are running your programm as a scheduled task ... did you give writing permissions to the target folder for the executing user of the task??
edit:
with permissions you can have 2 scenarios:
windows authenification
mixed authentification
if you are using windows authentification, the read and write permissions of the windows user are taken. otherwise the permissions for the sql server service account.
and this behaviour makes sense to me and maybe hits the nail in your scenario!
edit 2:
i don't want to encourage you to do so ... some admins may hate you when you mess up their acl's
but this may do the trick
btw: Magnus Johansson already gave you a "try-this" link
no matter for which method you go - be sure to hand in the correct user (as descriped above!)
(for full history)
...
side-note:
i know this is not the exact answer to your question, but i would recommend you smo to generate backups ...
like
using Microsoft.SqlServer.Management.Smo;
var bdi = new BackupDeviceItem(/* your path inlcuding desired file */);
var backup = new Backup
{
Database = /* name of the database */,
Initialize = true
};
backup.Devices.Add(bdi);
var server = new Server(this.SqlServer);
try
{
backup.SqlBackup(server);
}
catch (Exception ex)
{
// * log or sth
}
you only have to care for the .dll's. take assemblies for the desired server version (some params/properties vary through different server versions)
more info here
Ok Guys, Magnus and dittodhole! Thanks a lot for your help. I have combined Magnus'es link to the article on setting up permisions on the folder together with some more research and finally I've got it :).
So reassuming, I'm using Smo, and to create a folder with proper permissions I have to look for the group instead of win32_Users. Here you go a short snippet if someone finds this post he can find it usefull:
string tempPath = Directory.CreateDirectory("C:\\path_to_your_folder").FullName;
//set permissions
SelectQuery sQuery = new SelectQuery("Win32_Group",
"Domain='" +
System.Environment.UserDomainName.ToString() +
"'");
try
{
DirectoryInfo myDirectoryInfo = new DirectoryInfo("C:\\path_to_your_folder");
DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();
ManagementObjectSearcher mSearcher = new ManagementObjectSearcher(sQuery);
foreach (ManagementObject mObject in mSearcher.Get())
{
string User = System.Environment.UserDomainName + "\\" + mObject["Name"];
if(User.StartsWith("your-machine-name\\SQL"))
{
myDirectorySecurity.
AddAccessRule(new FileSystemAccessRule(User,
FileSystemRights.FullControl,
AccessControlType.Allow));
}
}
myDirectoryInfo.SetAccessControl(myDirectorySecurity);
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
Again thanks everyone for your help! Stackoverflow rocks!
Here is a procedure is use for back up in C#.Hope it helps
public void BackupDatabase (string BackUpLocation, string BackUpFileName, string
DatabaseName, string ServerName )
{
DatabaseName = "[" + DatabaseName + "]";
string fileUNQ = DateTime.Now.Day.ToString() + "_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year.ToString() +"_"+ DateTime.Now.Hour.ToString()+ DateTime.Now .Minute .ToString () + "_" + DateTime .Now .Second .ToString () ;
BackUpFileName = BackUpFileName + fileUNQ + ".bak";
string SQLBackUp = #"BACKUP DATABASE " + DatabaseName + " TO DISK = N'" + BackUpLocation + #"\" + BackUpFileName + #"'";
string svr = "Server=" + ServerName + ";Database=master;Integrated Security=True";
SqlConnection cnBk = new SqlConnection(svr);
SqlCommand cmdBkUp = new SqlCommand(SQLBackUp, cnBk);
try
{
cnBk.Open();
cmdBkUp.ExecuteNonQuery();
Label1.Text = "Done";
Label2.Text = SQLBackUp + " ######## Server name " + ServerName + " Database " + DatabaseName + " successfully backed up to " + BackUpLocation + #"\" + BackUpFileName + "\n Back Up Date : " + DateTime.Now.ToString();
}
catch (Exception ex)
{
Label1.Text = ex.ToString();
Label2.Text = SQLBackUp + " ######## Server name " + ServerName + " Database " + DatabaseName + " successfully backed up to " + BackUpLocation + #"\" + BackUpFileName + "\n Back Up Date : " + DateTime.Now.ToString();
}
finally
{
if (cnBk.State == ConnectionState.Open)
{
cnBk .Close();
}
}
}
Take a look at this article.
Remember to set the permissions for the account that the SQL Server instance is running with.
Although this may not answer your immediate question, I'd advice you to look into SQL Server Integration Services (SSIS). This looks like the exact thing SSIS was created for, and in the 2008 version there's the possibility to use C# code if needed, should the standard components not do what you need (earlier versions used VB.NET).
MSDN SSIS Info Link 1
SSIS 2005 Tutorial Link 2
Take a look at it.

Categories

Resources