Hi,
I need to copy a file from a remote machine using wmi
but i don't know how to define the path to my machine, below is the
code that makes a copy of a file in the same machine, what should be
the path if i wanted to copy it to my c:\example\ folder
ManagementBaseObject inParams, outParams;
foreach (ManagementObject mo in queryCollection)
{
Console.WriteLine(mo["Name"].ToString());
inParams = mo.GetMethodParameters("Copy");
inParams["FileName"]
= "c:\\example.config; <----------------- what can i
put here so that it refers to my machine
outParams = mo.InvokeMethod("Copy", inParams, null);
// List outParams
Console.WriteLine("Out parameters:");
Console.WriteLine("ReturnValue: " + outParams["ReturnValue"]);
}
-----EDIT:-----
So, i've searched and experimented the whole weekend and nothing.
When I execute this code i get "returnValue 9" wich according to the WMI documentation represents "Invalid object".
The operation is sucessfull if i try to copy between folder on the same machine.
I'm using Windows Server 2003 on both machines.
I don't know any other way to copy a file from the remote machine to my machine.
oq = new ObjectQuery(@"Select * from CIM_Datafile Where Extension = 'config' and Drive = 'c:' and FileName = 'machine' and path like '\\windows\\microsoft.net\\framework\\%\\config%'");
query = new ManagementObjectSearcher(ms, oq);
queryCollection = query.Get();
ManagementBaseObject inParams, outParams;
foreach (ManagementObject mo2 in queryCollection)
{
Console.WriteLine(mo2["Name"].ToString());
inParams = mo2.GetMethodParameters("Copy");
inParams["FileName"] = "\\\\ibm-server2003\\I$\\machine.config";
outParams = mo2.InvokeMethod("Copy", inParams, null);
// List outParams
Console.WriteLine("Out parameters:");
Console.WriteLine("ReturnValue: " + outParams["ReturnValue"]);
}

get file from remote machine using WMI
John.Doe
museicon
File.Copy(@"\\toshiba-win2003\C$\windows\microsoft.net\framework\v2.0.50727\config\machine.config",@"i:\machine.config");
I thought i needed some kind of permisson to use the method... seems not.
tkns for the help anyways.
Joeschoe
When I execute this code i get "returnValue 9" wich according to the WMI documentation represents "Invalid object".
The operation is sucessfull if i try to copy between folder on the same machine.
I'm using Windows Server 2003 on both machines.
I don't know any other way to copy a file from the remote machine to my machine.
oq = new ObjectQuery(@"Select * from CIM_Datafile Where Extension = 'config' and Drive = 'c:' and FileName = 'machine' and path like '\\windows\\microsoft.net\\framework\\%\\config%'");
query = new ManagementObjectSearcher(ms, oq);
queryCollection = query.Get();
ManagementBaseObject inParams, outParams;
foreach (ManagementObject mo2 in queryCollection)
{
Console.WriteLine(mo2["Name"].ToString());
inParams = mo2.GetMethodParameters("Copy");
inParams["FileName"] = "\\\\ibm-server2003\\I$\\machine.config";
outParams = mo2.InvokeMethod("Copy", inParams, null);
// List outParams
Console.WriteLine("Out parameters:");
Console.WriteLine("ReturnValue: " + outParams["ReturnValue"]);
}
stallion_alpa
no idea if this helps but take a look:
http://msdn.microsoft.com/library/default.asp url=/library/en-us/wmisdk/wmi/copy_method_in_class_win32_directory.asp
parivesh
why don't you map the remote machine to a drive (map network drive) then use the File.Copy() method in .NET to copy the file from the remote machine which has been mapped, to the local drive
is this not an option
Any ole Joe
well yes it is possible in Windows, just map the share drive then use File.Move class/method in .NET to copy the file
babarzhr
Can you tell me if this is possible
Gunnar Adler
well, make sure that the folder is shared (right click on folder > sharing & security and specify a share name)
then on the remote computer simply map the shared folder by right clicking my computer > map a network drive and enter the shared folder:
\\computername\ShareName
this will then make your shared folder available on the remote machine from which the remote machine can access and copy files to and from.
is this what you are after
Siteadm
Not mentioning that the application is multi-thread, if i do that, i'll have dozens of drives mapped at the same time.
bennett1016
I'm using WMI and at the moment i can copy the file between folders on the remote machine, but not from the remote machine to a folder on my local machine.
The application i'm developing is in C#.
The code approach i'm trying is this:
//this query is for testing porpose only
oq = new ObjectQuery(@"Select * from CIM_Datafile Where Extension = 'config' and FileName = 'machine' and path like '%\\windows\\microsoft.net\\framework\\%\\config%'");
query = new ManagementObjectSearcher(ms, oq);
queryCollection = query.Get();
ManagementBaseObject inParams, outParams;
foreach (ManagementObject mo2 in queryCollection)
{
Console.WriteLine(mo2["Name"].ToString());
inParams = mo2.GetMethodParameters("Copy");
inParams["FileName"] = "\\\\ibm-xp\\C$\\machine.config"; <--- This line doesn't work
outParams = mo2.InvokeMethod("Copy", inParams, null);
// List outParams
Console.WriteLine("Out parameters:");
Console.WriteLine("ReturnValue: " + outParams["ReturnValue"]);
break;
}
Frank Racis
SomeDeveloperPerson
try this:
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' AND FileName = 'machine'");// AND path LIKE '%\\win2000\\microsoft.net\\framework%'");
ObjectQuery oQuery = new ObjectQuery(sb.ToString());
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(scope, oQuery);
ManagementObjectCollection oReturnCollection = oSearcher.Get();
foreach (ManagementObject oReturn in oReturnCollection)
{
//MessageBox.Show(oReturn["Name"].ToString());
oReturn.InvokeMethod("Copy", new object[] { oReturn["Name"], "\\yourServer\\path\\filename.ext" });
}
Freqy
well the same approach would kind of also apply with WMI since it needs to some how reference the other machine...
ill see what i can dig up
LalitSRana
ok I see, I was confused!
so you want to copy a file from a remote computer TO your local computer correct
well this I believe can be done, as you have done, in WMI
do you know the commands in WMI itself (without implementing it in C#)
if so - are you able to post them
Then I can try to help you out integrating them into C# hopefully
try to see if this helps:
http://forums.devshed.com/net-development-87/invoking-copy-from-wmi-between-connections-130157.html