I'm trying to delete an existing file on the remote server using WMI.
Here's my code:
string name = @"\\servername\\OCROut\\basketball.txt";
ConnectionOptions options = new ConnectionOptions(remoteServer, "username", "password", "ntlmdomain:domainName", ImpersonationLevel.Impersonate, AuthenticationLevel.Default, true, null, System.TimeSpan.MaxValue);
ManagementScope scope = new ManagementScope("\\\\server\\root\\cimv2", options);
scope.Connect();
var query = new ObjectQuery(string.Format("SELECT * FROM CIM_Datafile WHERE Drive = 'D' AND Name = '{0}' AND Filename = 'basketball' and Extension = 'txt'", name));
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
var tobeDeleted = searcher.Get();
foreach (ManagementObject item in searcher.Get())
{
item.InvokeMethod("Delete", null);
}
The Query is working file but but my Count = 0 when i'm executing the searcher.Get() method. I tried everything, different slashes, without the drive, Filename and extension but nothing seem to be working and i know that the file exists.
Any help would be highly appreciated.