0

I wish to search for recently modified text file which has Indicator in the filename on remote machine in same domain. The (UNC) path for the directory looks like

\\MachineName\Applications\Indicator\myFolder

I tried something like this :

ConnectionOptions options = new ConnectionOptions();

options.Username = username;

options.Password = password;

ManagementScope scope = new ManagementScope("\\\\MachineName\\root\\cimv2", options);

scope.Connect(); // returns true i.e IsConnected=true

ObjectQuery query = new ObjectQuery(string.Format("SELECT * FROM CIM_DataFile where path=\\MachineName\Applications\Indicator\myFolder");    //This query is just for trial purpose.

ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

ManagementObjectCollection queryCollection = searcher.Get();      //Code breaks here

I get the following error :

'queryCollection.Count' threw an exception of type

'System.Management.ManagementException'

Message is InvalidQuery

Can someone help me with 2 things:

  1. Why is this simple code breaking?

  2. Can someone help me with WMI query to get the recently modified file in the specified directory?

PS: Please let me know if any additional info is needed. Thanks!

Community
  • 1
  • 1
m_beta
  • 132
  • 15
  • Is there a specific need for WMI ? – TheGeneral Sep 26 '19 at 06:03
  • @TheGeneral Since the files are placed on another remote machine, I'll have to connect with the same and fire the query. So WMI is the appropriate option right? Please let me know if I can achieve the same via any other way. – m_beta Sep 26 '19 at 06:33
  • **1st** unclear syntax of `new ObjectQuery(string.Format( …`; **2nd** the `path` property of `CIM_DataFile` shouldn't contain machine/drive and should contain a trailing backspace; the `where` clause in the query string _could_ read as `where path='\\Applications\\Indicator\\myFolder\\'` (note single quotes). – JosefZ Mar 23 '20 at 15:50

0 Answers0