Copying a folder on the server

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,
I am writing an app that will sit on the desktop. It needs to be able to
make a copy of a folder that sits on the server. It will copy the folder
from the server and place it in the same directory on the same server. This
is the line of code that I am using:

System.IO.File.Copy(sDistinationFolder + "NewOrderTemplate",
sDistinationFolder + myDate.Date + "_" + mtxtJobNumber.Text);

So, as you can see, I am copying a folder called 'NewOrderTemplate' and the
new copy will be called date_jobnumber.

When I try to run this I always get a permission denied. The root folder
has 'everyone' with full permissions.

Please help.

Thanks,
Michael.
 
Michael,

Everyone might have access to it, but does the assembly that is running
having access to it.

What you have to remember is that the code generated by C# is running in a
secured environment and you can no longer assume that security settings that
would work for a stand Win32 Application, would work for a C# Application,
especially when accessing resources that are not local to the running
assembly.

Regards
Scott Blood
C# Developer
 
Thanks Scott,
but what can I do about it? I need the app to run on different people's
PC, (in other words, I don't know who yet) and I need it to make that copy on
the server. (\\servername\folder1\foldertocopy).

Thanks,
Michael.
 
This has nothing to do with the OP's issue. What you are talking about is
Code Access Security, the OP's issue is "windows" security related, that is,
the user running the application has no access permissions to the remote
file server.

Willy.

| Michael,
|
| Everyone might have access to it, but does the assembly that is running
| having access to it.
|
| What you have to remember is that the code generated by C# is running in a
| secured environment and you can no longer assume that security settings
that
| would work for a stand Win32 Application, would work for a C# Application,
| especially when accessing resources that are not local to the running
| assembly.
|
| Regards
| Scott Blood
| C# Developer
|
| | > Hello,
| > I am writing an app that will sit on the desktop. It needs to be able
to
| > make a copy of a folder that sits on the server. It will copy the
folder
| > from the server and place it in the same directory on the same server.
| > This
| > is the line of code that I am using:
| >
| > System.IO.File.Copy(sDistinationFolder + "NewOrderTemplate",
| > sDistinationFolder + myDate.Date + "_" + mtxtJobNumber.Text);
| >
| > So, as you can see, I am copying a folder called 'NewOrderTemplate' and
| > the
| > new copy will be called date_jobnumber.
| >
| > When I try to run this I always get a permission denied. The root
folder
| > has 'everyone' with full permissions.
| >
| > Please help.
| >
| > Thanks,
| > Michael.
| >
|
|
 
This is quite complicated (and cumbersome) to achieve when you don't run in
a Windows domain realm.
One option is to create a common account on all, clients and server, having
the same credentials (user name & password).
Another option is to use System.Management and the WMI class CIM_LogicalFile
and it's Copy or CopyEx command. When using WMI you only need to specify a
server account wit appropriate privileges to the directory when connecting.


Willy.


| Thanks Scott,
| but what can I do about it? I need the app to run on different people's
| PC, (in other words, I don't know who yet) and I need it to make that copy
on
| the server. (\\servername\folder1\foldertocopy).
|
| Thanks,
| Michael.
|
|
|
|
| "scott blood" wrote:
|
| > Michael,
| >
| > Everyone might have access to it, but does the assembly that is running
| > having access to it.
| >
| > What you have to remember is that the code generated by C# is running in
a
| > secured environment and you can no longer assume that security settings
that
| > would work for a stand Win32 Application, would work for a C#
Application,
| > especially when accessing resources that are not local to the running
| > assembly.
| >
| > Regards
| > Scott Blood
| > C# Developer
| >
| > | > > Hello,
| > > I am writing an app that will sit on the desktop. It needs to be able
to
| > > make a copy of a folder that sits on the server. It will copy the
folder
| > > from the server and place it in the same directory on the same server.
| > > This
| > > is the line of code that I am using:
| > >
| > > System.IO.File.Copy(sDistinationFolder + "NewOrderTemplate",
| > > sDistinationFolder + myDate.Date + "_" + mtxtJobNumber.Text);
| > >
| > > So, as you can see, I am copying a folder called 'NewOrderTemplate'
and
| > > the
| > > new copy will be called date_jobnumber.
| > >
| > > When I try to run this I always get a permission denied. The root
folder
| > > has 'everyone' with full permissions.
| > >
| > > Please help.
| > >
| > > Thanks,
| > > Michael.
| > >
| >
| >
| >
 
Here's a complete sample illustrating the use of System.management to copy
files on a remote server.
This has the advantage that the file is copied locally (o the server)
without being passed back and forth to/from the workstation where this
program runs.

using System;
using System.Management;
public class Program{

public static void Main() {
string remMachine = "remoteservername"; // this is the name of the file
server
string fileName = "C:\\somedir\somefile"; // this is the input file
string objPath = "Cim_LogicalFile.Name='" + fileName + "'"; // watch the
single quotes!!
ConnectionOptions co = new ConnectionOptions();
// server account credentialswith appropriate privileges to in/out file
paths
co.Username = "user";
co.Password = "pwd";
ManagementScope scope = new ManagementScope(@"\\" + remMachine +
@"\root\cimv2", co);
using (ManagementObject remFilePath = new ManagementObject(objPath))
{
remFilePath.Scope = scope;
ManagementBaseObject inputArgs =
remFilePath.GetMethodParameters("Copy");
inputArgs["FileName"] = "C:\\somedir\\somecopy"; //this is the output
file
ManagementBaseObject outParams = remFilePath.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);
}
}
}


Willy.
 
Willy,
thanks for the help. But when I start to type in the code you gave me and
I get to the line: System.Management; The intellasence does not have the
word 'Management' in there. How do I access it? I am running VS2005.

Thanks,
Michael.
 
You need to add a reference to the System.Management.dll in your project
settings.

Willy.


| Willy,
| thanks for the help. But when I start to type in the code you gave me
and
| I get to the line: System.Management; The intellasence does not have the
| word 'Management' in there. How do I access it? I am running VS2005.
|
| Thanks,
| Michael.
|
| "Willy Denoyette [MVP]" wrote:
|
| > Here's a complete sample illustrating the use of System.management to
copy
| > files on a remote server.
| > This has the advantage that the file is copied locally (o the server)
| > without being passed back and forth to/from the workstation where this
| > program runs.
| >
| > using System;
| > using System.Management;
| > public class Program{
| >
| > public static void Main() {
| > string remMachine = "remoteservername"; // this is the name of the
file
| > server
| > string fileName = "C:\\somedir\somefile"; // this is the input file
| > string objPath = "Cim_LogicalFile.Name='" + fileName + "'"; // watch
the
| > single quotes!!
| > ConnectionOptions co = new ConnectionOptions();
| > // server account credentialswith appropriate privileges to in/out file
| > paths
| > co.Username = "user";
| > co.Password = "pwd";
| > ManagementScope scope = new ManagementScope(@"\\" + remMachine +
| > @"\root\cimv2", co);
| > using (ManagementObject remFilePath = new ManagementObject(objPath))
| > {
| > remFilePath.Scope = scope;
| > ManagementBaseObject inputArgs =
| > remFilePath.GetMethodParameters("Copy");
| > inputArgs["FileName"] = "C:\\somedir\\somecopy"; //this is the
output
| > file
| > ManagementBaseObject outParams = remFilePath.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);
| > }
| > }
| > }
| >
| >
| > Willy.
| >
| >
| >
| >
| >
 
Willy, this code seems to want to copy a file on that server. I am trying to
copy an entire folder on the server.

Thanks,
Michael.
 
Willy, this code seems to want to copy a file on that server. I am trying to
copy an entire folder on the server.

Thanks,
Michael.
 
No it copies whatever you specify as the input/output filename.
This will copy the somedir to a new folder copydir
string fileName = "c:\\somedir";
....
inputArgs["FileName"] = "c:\\copydir";

Please consult the documentation (System.Management and WMI) in MSDN, don't
use program samples blindly without knowing what it's actually doing.

Willy.



| Willy, this code seems to want to copy a file on that server. I am trying
to
| copy an entire folder on the server.
|
| Thanks,
| Michael.
|
|
| "Willy Denoyette [MVP]" wrote:
|
| > You need to add a reference to the System.Management.dll in your project
| > settings.
| >
| > Willy.
| >
| >
| > | > | Willy,
| > | thanks for the help. But when I start to type in the code you gave
me
| > and
| > | I get to the line: System.Management; The intellasence does not have
the
| > | word 'Management' in there. How do I access it? I am running VS2005.
| > |
| > | Thanks,
| > | Michael.
| > |
| > | "Willy Denoyette [MVP]" wrote:
| > |
| > | > Here's a complete sample illustrating the use of System.management
to
| > copy
| > | > files on a remote server.
| > | > This has the advantage that the file is copied locally (o the
server)
| > | > without being passed back and forth to/from the workstation where
this
| > | > program runs.
| > | >
| > | > using System;
| > | > using System.Management;
| > | > public class Program{
| > | >
| > | > public static void Main() {
| > | > string remMachine = "remoteservername"; // this is the name of the
| > file
| > | > server
| > | > string fileName = "C:\\somedir\somefile"; // this is the input
file
| > | > string objPath = "Cim_LogicalFile.Name='" + fileName + "'"; //
watch
| > the
| > | > single quotes!!
| > | > ConnectionOptions co = new ConnectionOptions();
| > | > // server account credentialswith appropriate privileges to in/out
file
| > | > paths
| > | > co.Username = "user";
| > | > co.Password = "pwd";
| > | > ManagementScope scope = new ManagementScope(@"\\" + remMachine +
| > | > @"\root\cimv2", co);
| > | > using (ManagementObject remFilePath = new
ManagementObject(objPath))
| > | > {
| > | > remFilePath.Scope = scope;
| > | > ManagementBaseObject inputArgs =
| > | > remFilePath.GetMethodParameters("Copy");
| > | > inputArgs["FileName"] = "C:\\somedir\\somecopy"; //this is the
| > output
| > | > file
| > | > ManagementBaseObject outParams =
remFilePath.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);
| > | > }
| > | > }
| > | > }
| > | >
| > | >
| > | > Willy.
| > | >
| > | >
| > | >
| > | >
| > | >
| >
| >
| >
 
Hi Willy,
a couple of questions:
1. this line:
ManagementScope scope = new ManagementScope(@"\\" + remMachine +
@"\root\cimv2", co);
seems to reference a folder on the root called 'cimv2'. I don't have a
folder like that. Do I need one?

2. this line:
ManagementBaseObject inputArgs = remFilePath.GetMethodParameters("Copy");
always gives me an 'access denied' error. Any thoughts?

Thanks,
Michael


Willy Denoyette said:
No it copies whatever you specify as the input/output filename.
This will copy the somedir to a new folder copydir
string fileName = "c:\\somedir";
....
inputArgs["FileName"] = "c:\\copydir";

Please consult the documentation (System.Management and WMI) in MSDN, don't
use program samples blindly without knowing what it's actually doing.

Willy.



| Willy, this code seems to want to copy a file on that server. I am trying
to
| copy an entire folder on the server.
|
| Thanks,
| Michael.
|
|
| "Willy Denoyette [MVP]" wrote:
|
| > You need to add a reference to the System.Management.dll in your project
| > settings.
| >
| > Willy.
| >
| >
| > | > | Willy,
| > | thanks for the help. But when I start to type in the code you gave
me
| > and
| > | I get to the line: System.Management; The intellasence does not have
the
| > | word 'Management' in there. How do I access it? I am running VS2005.
| > |
| > | Thanks,
| > | Michael.
| > |
| > | "Willy Denoyette [MVP]" wrote:
| > |
| > | > Here's a complete sample illustrating the use of System.management
to
| > copy
| > | > files on a remote server.
| > | > This has the advantage that the file is copied locally (o the
server)
| > | > without being passed back and forth to/from the workstation where
this
| > | > program runs.
| > | >
| > | > using System;
| > | > using System.Management;
| > | > public class Program{
| > | >
| > | > public static void Main() {
| > | > string remMachine = "remoteservername"; // this is the name of the
| > file
| > | > server
| > | > string fileName = "C:\\somedir\somefile"; // this is the input
file
| > | > string objPath = "Cim_LogicalFile.Name='" + fileName + "'"; //
watch
| > the
| > | > single quotes!!
| > | > ConnectionOptions co = new ConnectionOptions();
| > | > // server account credentialswith appropriate privileges to in/out
file
| > | > paths
| > | > co.Username = "user";
| > | > co.Password = "pwd";
| > | > ManagementScope scope = new ManagementScope(@"\\" + remMachine +
| > | > @"\root\cimv2", co);
| > | > using (ManagementObject remFilePath = new
ManagementObject(objPath))
| > | > {
| > | > remFilePath.Scope = scope;
| > | > ManagementBaseObject inputArgs =
| > | > remFilePath.GetMethodParameters("Copy");
| > | > inputArgs["FileName"] = "C:\\somedir\\somecopy"; //this is the
| > output
| > | > file
| > | > ManagementBaseObject outParams =
remFilePath.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);
| > | > }
| > | > }
| > | > }
| > | >
| > | >
| > | > Willy.
| > | >
| > | >
| > | >
| > | >
| > | >
| >
| >
| >
 
| Hi Willy,
| a couple of questions:
| 1. this line:
| ManagementScope scope = new ManagementScope(@"\\" + remMachine +
| @"\root\cimv2", co);
| seems to reference a folder on the root called 'cimv2'. I don't have a
| folder like that. Do I need one?
|

No it does not refer to a folder , it refers to a WMI namespace.
The forder or file you are refereing to is specified in the object path:

string fileName = "C:\\somedir"; // this is the input file
string objPath = "Cim_LogicalFile.Name='" + fileName + "'"; // HER file name
is the input path



| 2. this line:
| ManagementBaseObject inputArgs = remFilePath.GetMethodParameters("Copy");
| always gives me an 'access denied' error. Any thoughts?
|

You need to "remote enable" the WMI namespace on the server, run wmimgmt.msc
and change the security settings on root\cimv2 make sure that the user
account used to connect has remote enable permissions.

| Thanks,
| Michael
|
|
| "Willy Denoyette [MVP]" wrote:
|
| > No it copies whatever you specify as the input/output filename.
| > This will copy the somedir to a new folder copydir
| > string fileName = "c:\\somedir";
| > ....
| > inputArgs["FileName"] = "c:\\copydir";
| >
| > Please consult the documentation (System.Management and WMI) in MSDN,
don't
| > use program samples blindly without knowing what it's actually doing.
| >
| > Willy.
| >
| >
| >
| > | > | Willy, this code seems to want to copy a file on that server. I am
trying
| > to
| > | copy an entire folder on the server.
| > |
| > | Thanks,
| > | Michael.
| > |
| > |
| > | "Willy Denoyette [MVP]" wrote:
| > |
| > | > You need to add a reference to the System.Management.dll in your
project
| > | > settings.
| > | >
| > | > Willy.
| > | >
| > | >
| > | > | > | > | Willy,
| > | > | thanks for the help. But when I start to type in the code you
gave
| > me
| > | > and
| > | > | I get to the line: System.Management; The intellasence does not
have
| > the
| > | > | word 'Management' in there. How do I access it? I am running
VS2005.
| > | > |
| > | > | Thanks,
| > | > | Michael.
| > | > |
| > | > | "Willy Denoyette [MVP]" wrote:
| > | > |
| > | > | > Here's a complete sample illustrating the use of
System.management
| > to
| > | > copy
| > | > | > files on a remote server.
| > | > | > This has the advantage that the file is copied locally (o the
| > server)
| > | > | > without being passed back and forth to/from the workstation
where
| > this
| > | > | > program runs.
| > | > | >
| > | > | > using System;
| > | > | > using System.Management;
| > | > | > public class Program{
| > | > | >
| > | > | > public static void Main() {
| > | > | > string remMachine = "remoteservername"; // this is the name of
the
| > | > file
| > | > | > server
| > | > | > string fileName = "C:\\somedir\somefile"; // this is the input
| > file
| > | > | > string objPath = "Cim_LogicalFile.Name='" + fileName + "'"; //
| > watch
| > | > the
| > | > | > single quotes!!
| > | > | > ConnectionOptions co = new ConnectionOptions();
| > | > | > // server account credentialswith appropriate privileges to
in/out
| > file
| > | > | > paths
| > | > | > co.Username = "user";
| > | > | > co.Password = "pwd";
| > | > | > ManagementScope scope = new ManagementScope(@"\\" + remMachine
+
| > | > | > @"\root\cimv2", co);
| > | > | > using (ManagementObject remFilePath = new
| > ManagementObject(objPath))
| > | > | > {
| > | > | > remFilePath.Scope = scope;
| > | > | > ManagementBaseObject inputArgs =
| > | > | > remFilePath.GetMethodParameters("Copy");
| > | > | > inputArgs["FileName"] = "C:\\somedir\\somecopy"; //this is
the
| > | > output
| > | > | > file
| > | > | > ManagementBaseObject outParams =
| > remFilePath.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);
| > | > | > }
| > | > | > }
| > | > | > }
| > | > | >
| > | > | >
| > | > | > Willy.
| > | > | >
| > | > | >
| > | > | >
| > | > | >
| > | > | >
| > | >
| > | >
| > | >
| >
| >
| >
 
Hi Willy,
thanks. It does not seem to copy all the subfolders. Is that an argument
that I can set?

Michael.

Willy Denoyette said:
| Hi Willy,
| a couple of questions:
| 1. this line:
| ManagementScope scope = new ManagementScope(@"\\" + remMachine +
| @"\root\cimv2", co);
| seems to reference a folder on the root called 'cimv2'. I don't have a
| folder like that. Do I need one?
|

No it does not refer to a folder , it refers to a WMI namespace.
The forder or file you are refereing to is specified in the object path:

string fileName = "C:\\somedir"; // this is the input file
string objPath = "Cim_LogicalFile.Name='" + fileName + "'"; // HER file name
is the input path



| 2. this line:
| ManagementBaseObject inputArgs = remFilePath.GetMethodParameters("Copy");
| always gives me an 'access denied' error. Any thoughts?
|

You need to "remote enable" the WMI namespace on the server, run wmimgmt.msc
and change the security settings on root\cimv2 make sure that the user
account used to connect has remote enable permissions.

| Thanks,
| Michael
|
|
| "Willy Denoyette [MVP]" wrote:
|
| > No it copies whatever you specify as the input/output filename.
| > This will copy the somedir to a new folder copydir
| > string fileName = "c:\\somedir";
| > ....
| > inputArgs["FileName"] = "c:\\copydir";
| >
| > Please consult the documentation (System.Management and WMI) in MSDN,
don't
| > use program samples blindly without knowing what it's actually doing.
| >
| > Willy.
| >
| >
| >
| > | > | Willy, this code seems to want to copy a file on that server. I am
trying
| > to
| > | copy an entire folder on the server.
| > |
| > | Thanks,
| > | Michael.
| > |
| > |
| > | "Willy Denoyette [MVP]" wrote:
| > |
| > | > You need to add a reference to the System.Management.dll in your
project
| > | > settings.
| > | >
| > | > Willy.
| > | >
| > | >
| > | > | > | > | Willy,
| > | > | thanks for the help. But when I start to type in the code you
gave
| > me
| > | > and
| > | > | I get to the line: System.Management; The intellasence does not
have
| > the
| > | > | word 'Management' in there. How do I access it? I am running
VS2005.
| > | > |
| > | > | Thanks,
| > | > | Michael.
| > | > |
| > | > | "Willy Denoyette [MVP]" wrote:
| > | > |
| > | > | > Here's a complete sample illustrating the use of
System.management
| > to
| > | > copy
| > | > | > files on a remote server.
| > | > | > This has the advantage that the file is copied locally (o the
| > server)
| > | > | > without being passed back and forth to/from the workstation
where
| > this
| > | > | > program runs.
| > | > | >
| > | > | > using System;
| > | > | > using System.Management;
| > | > | > public class Program{
| > | > | >
| > | > | > public static void Main() {
| > | > | > string remMachine = "remoteservername"; // this is the name of
the
| > | > file
| > | > | > server
| > | > | > string fileName = "C:\\somedir\somefile"; // this is the input
| > file
| > | > | > string objPath = "Cim_LogicalFile.Name='" + fileName + "'"; //
| > watch
| > | > the
| > | > | > single quotes!!
| > | > | > ConnectionOptions co = new ConnectionOptions();
| > | > | > // server account credentialswith appropriate privileges to
in/out
| > file
| > | > | > paths
| > | > | > co.Username = "user";
| > | > | > co.Password = "pwd";
| > | > | > ManagementScope scope = new ManagementScope(@"\\" + remMachine
+
| > | > | > @"\root\cimv2", co);
| > | > | > using (ManagementObject remFilePath = new
| > ManagementObject(objPath))
| > | > | > {
| > | > | > remFilePath.Scope = scope;
| > | > | > ManagementBaseObject inputArgs =
| > | > | > remFilePath.GetMethodParameters("Copy");
| > | > | > inputArgs["FileName"] = "C:\\somedir\\somecopy"; //this is
the
| > | > output
| > | > | > file
| > | > | > ManagementBaseObject outParams =
| > remFilePath.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);
| > | > | > }
| > | > | > }
| > | > | > }
| > | > | >
| > | > | >
| > | > | > Willy.
| > | > | >
| > | > | >
| > | > | >
| > | > | >
| > | > | >
| > | >
| > | >
| > | >
| >
| >
| >
 
Yes, there is, use the CopyEx method and set the Recursive argument to true.
And take a look at the WMI classes in MSDN....


ManagementBaseObject inputArgs = filePath.GetMethodParameters("CopyEx");
inputArgs["FileName"] = ".......";
inputArgs["Recursive"] = true;
ManagementBaseObject outParams = filePath.InvokeMethod("CopyEx",
inputArgs, null);

Willy.

| Hi Willy,
| thanks. It does not seem to copy all the subfolders. Is that an argument
| that I can set?
|
| Michael.
|
| "Willy Denoyette [MVP]" wrote:
|
| >
| > | > | Hi Willy,
| > | a couple of questions:
| > | 1. this line:
| > | ManagementScope scope = new ManagementScope(@"\\" + remMachine +
| > | @"\root\cimv2", co);
| > | seems to reference a folder on the root called 'cimv2'. I don't have
a
| > | folder like that. Do I need one?
| > |
| >
| > No it does not refer to a folder , it refers to a WMI namespace.
| > The forder or file you are refereing to is specified in the object path:
| >
| > string fileName = "C:\\somedir"; // this is the input file
| > string objPath = "Cim_LogicalFile.Name='" + fileName + "'"; // HER file
name
| > is the input path
| >
| >
| >
| > | 2. this line:
| > | ManagementBaseObject inputArgs =
remFilePath.GetMethodParameters("Copy");
| > | always gives me an 'access denied' error. Any thoughts?
| > |
| >
| > You need to "remote enable" the WMI namespace on the server, run
wmimgmt.msc
| > and change the security settings on root\cimv2 make sure that the user
| > account used to connect has remote enable permissions.
| >
| > | Thanks,
| > | Michael
| > |
| > |
| > | "Willy Denoyette [MVP]" wrote:
| > |
| > | > No it copies whatever you specify as the input/output filename.
| > | > This will copy the somedir to a new folder copydir
| > | > string fileName = "c:\\somedir";
| > | > ....
| > | > inputArgs["FileName"] = "c:\\copydir";
| > | >
| > | > Please consult the documentation (System.Management and WMI) in
MSDN,
| > don't
| > | > use program samples blindly without knowing what it's actually
doing.
| > | >
| > | > Willy.
| > | >
| > | >
| > | >
| > | > | > | > | Willy, this code seems to want to copy a file on that server. I
am
| > trying
| > | > to
| > | > | copy an entire folder on the server.
| > | > |
| > | > | Thanks,
| > | > | Michael.
| > | > |
| > | > |
| > | > | "Willy Denoyette [MVP]" wrote:
| > | > |
| > | > | > You need to add a reference to the System.Management.dll in your
| > project
| > | > | > settings.
| > | > | >
| > | > | > Willy.
| > | > | >
| > | > | >
| > | > | > | > | > | > | Willy,
| > | > | > | thanks for the help. But when I start to type in the code
you
| > gave
| > | > me
| > | > | > and
| > | > | > | I get to the line: System.Management; The intellasence does
not
| > have
| > | > the
| > | > | > | word 'Management' in there. How do I access it? I am running
| > VS2005.
| > | > | > |
| > | > | > | Thanks,
| > | > | > | Michael.
| > | > | > |
| > | > | > | "Willy Denoyette [MVP]" wrote:
| > | > | > |
| > | > | > | > Here's a complete sample illustrating the use of
| > System.management
| > | > to
| > | > | > copy
| > | > | > | > files on a remote server.
| > | > | > | > This has the advantage that the file is copied locally (o
the
| > | > server)
| > | > | > | > without being passed back and forth to/from the workstation
| > where
| > | > this
| > | > | > | > program runs.
| > | > | > | >
| > | > | > | > using System;
| > | > | > | > using System.Management;
| > | > | > | > public class Program{
| > | > | > | >
| > | > | > | > public static void Main() {
| > | > | > | > string remMachine = "remoteservername"; // this is the
name of
| > the
| > | > | > file
| > | > | > | > server
| > | > | > | > string fileName = "C:\\somedir\somefile"; // this is the
input
| > | > file
| > | > | > | > string objPath = "Cim_LogicalFile.Name='" + fileName +
"'"; //
| > | > watch
| > | > | > the
| > | > | > | > single quotes!!
| > | > | > | > ConnectionOptions co = new ConnectionOptions();
| > | > | > | > // server account credentialswith appropriate privileges to
| > in/out
| > | > file
| > | > | > | > paths
| > | > | > | > co.Username = "user";
| > | > | > | > co.Password = "pwd";
| > | > | > | > ManagementScope scope = new ManagementScope(@"\\" +
remMachine
| > +
| > | > | > | > @"\root\cimv2", co);
| > | > | > | > using (ManagementObject remFilePath = new
| > | > ManagementObject(objPath))
| > | > | > | > {
| > | > | > | > remFilePath.Scope = scope;
| > | > | > | > ManagementBaseObject inputArgs =
| > | > | > | > remFilePath.GetMethodParameters("Copy");
| > | > | > | > inputArgs["FileName"] = "C:\\somedir\\somecopy"; //this
is
| > the
| > | > | > output
| > | > | > | > file
| > | > | > | > ManagementBaseObject outParams =
| > | > remFilePath.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);
| > | > | > | > }
| > | > | > | > }
| > | > | > | > }
| > | > | > | >
| > | > | > | >
| > | > | > | > Willy.
| > | > | > | >
| > | > | > | >
| > | > | > | >
| > | > | > | >
| > | > | > | >
| > | > | >
| > | > | >
| > | > | >
| > | >
| > | >
| > | >
| >
| >
| >
 
Willy,

I need to do something similar, but have no idea of how to use the WMI
class. Could you email me, and possible let me ask you some advice on how to
get started?

I have looked over the MSDN info on the WMI, and can create a class that
compiles, but I have no idea how to instantiate it.

PS, I use VB, but ccan read C#
 
Back
Top