none
Searching for a File on remote machine using WMI C# RRS feed

  • Question

  • I want to search for a file on remote machine. I don't know the exact file path but I know its under C:\SomeFolder

    My query is something like this in WMI

    string querystr = "SELECT * FROM CIM_DataFile Where Path='C:\\SomeFolder'";
    ObjectQuery query = new ObjectQuery(querystr );
    ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, query);

    I get invalid query error.

    Any idea on how to achieve this ? Looks like there is no "Under" keyword.



    • Edited by Shawn Dan Tuesday, October 23, 2012 3:26 AM
    Tuesday, October 23, 2012 3:17 AM

Answers

  • Well

    1) that would require that drive or parent folder to be shared out because you specify remote machine \\MyServer\\Directory

    2) I also want to grab file properties like createtime etc. Not sure if this allows it.

    Sharing is not necessary. For example, if you want to search the folder 'c:\SomeDirectory' on MyServer, then you can give the path as \\MyServer\c$\SomeDirectory. Just ensure that you have proper credentials to access C drive of MyServer.

    Of course you can get CreateTime or other information as well.

    var Files = Directory.GetFiles(searchPath, "*.*", SearchOption.AllDirectories);
    var file = Files.First(f => Path.GetFileName(f).ToUpper().Equals(searchFile.ToUpper()));
    
    if (String.IsNullOrEmpty(file))
    {
        MessageBox.Show("File Not Found");
    }
    else
    {
        FileInfo fi = new FileInfo(file);
        DateTime CreationDtTm = fi.CreationTime;  //Creattion Time 
        DateTime LastAccessTm = fi.LastAccessTime;
    }
    You will also get a bunch of other information from FIleInfo object. Just give a try.

    Please mark this post as answer if it solved your problem. Happy Programming!

    Tuesday, October 23, 2012 5:17 AM

All replies

  • Why WMI for that? You can use regular File and Folder classes ,

    var Files = Directory.GetFiles(searchPath, "*.*", SearchOption.AllDirectories);
    var file = Files.First(f => Path.GetFileName(f).ToUpper().Equals(searchFile.ToUpper()));
    if (String.IsNullOrEmpty(file))
    {
        MessageBox.Show("File Not Found");
    }
    else
    {
        MessageBox.Show("File - ", file);
    }
    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    Tuesday, October 23, 2012 3:43 AM
  • Well

    1) that would require that drive or parent folder to be shared out because you specify remote machine \\MyServer\\Directory

    2) I also want to grab file properties like createtime etc. Not sure if this allows it.

    Tuesday, October 23, 2012 3:53 AM
  • Well

    1) that would require that drive or parent folder to be shared out because you specify remote machine \\MyServer\\Directory

    2) I also want to grab file properties like createtime etc. Not sure if this allows it.

    Sharing is not necessary. For example, if you want to search the folder 'c:\SomeDirectory' on MyServer, then you can give the path as \\MyServer\c$\SomeDirectory. Just ensure that you have proper credentials to access C drive of MyServer.

    Of course you can get CreateTime or other information as well.

    var Files = Directory.GetFiles(searchPath, "*.*", SearchOption.AllDirectories);
    var file = Files.First(f => Path.GetFileName(f).ToUpper().Equals(searchFile.ToUpper()));
    
    if (String.IsNullOrEmpty(file))
    {
        MessageBox.Show("File Not Found");
    }
    else
    {
        FileInfo fi = new FileInfo(file);
        DateTime CreationDtTm = fi.CreationTime;  //Creattion Time 
        DateTime LastAccessTm = fi.LastAccessTime;
    }
    You will also get a bunch of other information from FIleInfo object. Just give a try.

    Please mark this post as answer if it solved your problem. Happy Programming!

    Tuesday, October 23, 2012 5:17 AM