Results 1 to 9 of 9

Thread: WMI C# help

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    WMI C# help

    I'm new to WMI/C# using the System.Management namespace.
    basically, I want to retrieve a list of files from a remote computer (local network). I can get this done but the next stage is to copy a specific file over from the computer to a local folder....

    how would I do this?

    Code:
     System.Text.StringBuilder sb = new StringBuilder();
                ConnectionOptions oConn = new ConnectionOptions();
                ManagementScope scope = new ManagementScope("\\\\server\\root\\cimv2", oConn);
    
                sb.Append("SELECT * FROM CIM_Datafile WHERE Extension = 'config'");
    
    
                ObjectQuery oQuery = new ObjectQuery(sb.ToString());
                ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(scope, oQuery);
                ManagementObjectCollection oReturnCollection = oSearcher.Get();
    
                int rowCount = 0;
                foreach (ManagementObject oReturn in oReturnCollection)
                {
                    rowCount++;
                    MessageBox.Show(oReturn["Name"].ToString());
                }

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2
    New Member tracernet_v2's Avatar
    Join Date
    Aug 2006
    Posts
    5

    Re: WMI C# help

    You can use the WMI methods for copying files. The WMI class CIM_DataFile has Copy and CopyEx methods which you can use to copy files using WMI. You might want to use CopyEx since you can have a recursive mode of copying.

    Also, I think you can just copy the files using the other classes of the .NET framework. Just do what you think is easier.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: WMI C# help

    well I cant do it from .NET as .NET will not accept UNC paths hence why I had to go to WMI
    I tried CopyFile, seemed to have worked but nothing was copied

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  4. #4
    New Member tracernet_v2's Avatar
    Join Date
    Aug 2006
    Posts
    5

    Re: WMI C# help

    Quote Originally Posted by Techno
    well I cant do it from .NET as .NET will not accept UNC paths hence why I had to go to WMI
    I tried CopyFile, seemed to have worked but nothing was copied
    Have you tried CopyEx?? how did you code that one??

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: WMI C# help

    actually i managed to get it EXCEPT that it wont copy the file! (using copy or copyex) nothing happens, no exceptions, nothing.

    oReturn.InvokeMethod("Copy", new object[] { oReturn["Name"], "\\LocalComp\\C$"});

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  6. #6
    New Member tracernet_v2's Avatar
    Join Date
    Aug 2006
    Posts
    5

    Re: WMI C# help

    I believe the third parameter of the InvokeMethod is for the InvokeMethodOptions class. What .NET framework are you using??

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: WMI C# help

    ive actually got this to work now, thanks. I do have another Q though, I want to now invoke a process, start a process (run an app) - how would I do this in C# and WMI? I know in WMI there is a Create process, but how do I invoke this in C#?

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  8. #8
    New Member
    Join Date
    Apr 2007
    Posts
    1

    Unhappy Re: WMI C# help

    Please send me the code that worked for the copy, as I can't do it till now, I have the same problem and my code doesn't work

  9. #9
    New Member
    Join Date
    May 2008
    Posts
    1

    Re: WMI C# help

    Quote Originally Posted by Techno
    ive actually got this to work now, thanks. I do have another Q though, I want to now invoke a process, start a process (run an app) - how would I do this in C# and WMI? I know in WMI there is a Create process, but how do I invoke this in C#?
    It is customary to share with the forum the solution that worked for you. Otherwise, this post is just google-spam (A search that yields my problem, but not my solution).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width