By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,171 Members | 1,390 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,171 IT Pros & Developers. It's quick & easy.

Copying a folder on the server

P: n/a
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.

Mar 30 '06 #1
Share this Question
Share on Google+
17 Replies


P: n/a
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

"Michael" <Mi*****@discussions.microsoft.com> wrote in message
news:9A**********************************@microsof t.com...
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.

Mar 30 '06 #2

P: n/a
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

"Michael" <Mi*****@discussions.microsoft.com> wrote in message
news:9A**********************************@microsof t.com...
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.


Mar 30 '06 #3

P: n/a
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.

"scott blood" <sc*********@hotmail.com> wrote in message
news:O3**************@TK2MSFTNGP12.phx.gbl...
| 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
|
| "Michael" <Mi*****@discussions.microsoft.com> wrote in message
| news:9A**********************************@microsof t.com...
| > 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.
| >
|
|
Mar 30 '06 #4

P: n/a
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.
"Michael" <Mi*****@discussions.microsoft.com> wrote in message
news:49**********************************@microsof t.com...
| 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
| >
| > "Michael" <Mi*****@discussions.microsoft.com> wrote in message
| > news:9A**********************************@microsof t.com...
| > > 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.
| > >
| >
| >
| >
Mar 30 '06 #5

P: n/a
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.


Mar 30 '06 #6

P: n/a
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.


Mar 30 '06 #7

P: n/a
You need to add a reference to the System.Management.dll in your project
settings.

Willy.
"Michael" <Mi*****@discussions.microsoft.com> wrote in message
news:81**********************************@microsof t.com...
| 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.
| >
| >
| >
| >
| >
Mar 30 '06 #8

P: n/a
Hi Willy,
can you have "Console.writeline" in a windows form app?
Michael.

"Willy Denoyette [MVP]" wrote:
You need to add a reference to the System.Management.dll in your project
settings.

Willy.
"Michael" <Mi*****@discussions.microsoft.com> wrote in message
news:81**********************************@microsof t.com...
| 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.
| >
| >
| >
| >
| >

Mar 30 '06 #9

P: n/a
Hi Willy,
can you have "Console.writeline" in a windows form app?
Michael.

"Willy Denoyette [MVP]" wrote:
You need to add a reference to the System.Management.dll in your project
settings.

Willy.
"Michael" <Mi*****@discussions.microsoft.com> wrote in message
news:81**********************************@microsof t.com...
| 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.
| >
| >
| >
| >
| >

Mar 30 '06 #10

P: n/a
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.
"Michael" <Mi*****@discussions.microsoft.com> wrote in message
news:81**********************************@microsof t.com...
| 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.
| >
| >
| >
| >
| >

Mar 30 '06 #11

P: n/a
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.
"Michael" <Mi*****@discussions.microsoft.com> wrote in message
news:81**********************************@microsof t.com...
| 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.
| >
| >
| >
| >
| >

Mar 30 '06 #12

P: n/a
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.

"Michael" <Mi*****@discussions.microsoft.com> wrote in message
news:60**********************************@microsof t.com...
| 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.
| >
| >
| > "Michael" <Mi*****@discussions.microsoft.com> wrote in message
| > news:81**********************************@microsof t.com...
| > | 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.
| > | >
| > | >
| > | >
| > | >
| > | >
| >
| >
| >
Mar 31 '06 #13

P: n/a
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 [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.

"Michael" <Mi*****@discussions.microsoft.com> wrote in message
news:60**********************************@microsof t.com...
| 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.
| >
| >
| > "Michael" <Mi*****@discussions.microsoft.com> wrote in message
| > news:81**********************************@microsof t.com...
| > | 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.
| > | >
| > | >
| > | >
| > | >
| > | >
| >
| >
| >

Mar 31 '06 #14

P: n/a

"Michael" <Mi*****@discussions.microsoft.com> wrote in message
news:4F**********************************@microsof t.com...
| 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.
| >
| >
| >
| > "Michael" <Mi*****@discussions.microsoft.com> wrote in message
| > news:60**********************************@microsof t.com...
| > | 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.
| > | >
| > | >
| > | > "Michael" <Mi*****@discussions.microsoft.com> wrote in message
| > | > news:81**********************************@microsof t.com...
| > | > | 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.
| > | > | >
| > | > | >
| > | > | >
| > | > | >
| > | > | >
| > | >
| > | >
| > | >
| >
| >
| >
Apr 1 '06 #15

P: n/a
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:

"Michael" <Mi*****@discussions.microsoft.com> wrote in message
news:4F**********************************@microsof t.com...
| 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.
| >
| >
| >
| > "Michael" <Mi*****@discussions.microsoft.com> wrote in message
| > news:60**********************************@microsof t.com...
| > | 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.
| > | >
| > | >
| > | > "Michael" <Mi*****@discussions.microsoft.com> wrote in message
| > | > news:81**********************************@microsof t.com...
| > | > | 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.
| > | > | >
| > | > | >
| > | > | >
| > | > | >
| > | > | >
| > | >
| > | >
| > | >
| >
| >
| >

Apr 3 '06 #16

P: n/a
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.

"Michael" <Mi*****@discussions.microsoft.com> wrote in message
news:73**********************************@microsof t.com...
| 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:
|
| >
| > "Michael" <Mi*****@discussions.microsoft.com> wrote in message
| > news:4F**********************************@microsof t.com...
| > | 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.
| > | >
| > | >
| > | >
| > | > "Michael" <Mi*****@discussions.microsoft.com> wrote in message
| > | > news:60**********************************@microsof t.com...
| > | > | 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.
| > | > | >
| > | > | >
| > | > | > "Michael" <Mi*****@discussions.microsoft.com> wrote in message
| > | > | > news:81**********************************@microsof t.com...
| > | > | > | 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.
| > | > | > | >
| > | > | > | >
| > | > | > | >
| > | > | > | >
| > | > | > | >
| > | > | >
| > | > | >
| > | > | >
| > | >
| > | >
| > | >
| >
| >
| >
Apr 4 '06 #17

P: n/a
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#

"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.


Jun 5 '06 #18

This discussion thread is closed

Replies have been disabled for this discussion.