Connect to WMI remotely to Copy File from Server to Local Computer

Below a function which remote to server to copy a file to local pc through Windows Management Infrastructure (WMI).

public static void copyfiletolocal(ManagementScope scope)
        {

            ObjectQuery oq = new ObjectQuery(@"Select * from CIM_Datafile WHERE Extension = 'txt' AND FileName = 'FileName'");

            ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(scope, oq);

            using (ManagementObjectCollection oReturns = oSearcher.Get())
                foreach(ManagementObject oReturn in oReturns)
                {
                    oReturn.Scope = scope;
                    ManagementBaseObject inputArgs = oReturn.GetMethodParameters("Copy");
                    inputArgs["FileName"] = @"C:\\";
                    ManagementBaseObject outParams = oReturn.InvokeMethod("Copy",inputArgs,null);
                    uint ret = (uint)(outParams.Properties["ReturnValue"].Value);
                    if (ret == 0)
                    {
                        Console.WriteLine("success");
                    }
                    else
                    {
                        Console.WriteLine("Failed with error code: {0}", ret);
                    }
                }

        }

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.