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. 17 2886
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.
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.
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.
| >
|
|
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.
| > >
| >
| >
| >
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.
"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.
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.
| >
| >
| >
| >
| >
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. | > | > | > | > | >
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. | > | > | > | > | >
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. | > | > | > | > | >
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. | > | > | > | > | >
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.
| > | >
| > | >
| > | >
| > | >
| > | >
| >
| >
| >
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. | > | > | > | > | > | > | > | > | > | > | > | > | >
"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.
| > | > | >
| > | > | >
| > | > | >
| > | > | >
| > | > | >
| > | >
| > | >
| > | >
| >
| >
| >
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. | > | > | > | > | > | > | > | > | > | > | > | > | > | > | > | > | > | > | > | > | > | > | > | >
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.
| > | > | > | >
| > | > | > | >
| > | > | > | >
| > | > | > | >
| > | > | > | >
| > | > | >
| > | > | >
| > | > | >
| > | >
| > | >
| > | >
| >
| >
| >
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.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: 21novembre |
last post by:
Hi all,
I made a question several days before to describe my strange trouble of
mysqldump. But I still can't figour it out. Well, I just want to ask
another question whether I could just backup...
|
by: Mr 071 |
last post by:
I am trying to use shadow copying provided by the framework, but I am not
able to get it to work. More specifically, after a new app domain is created
and the assembly loaded in it, it is still not...
|
by: surya |
last post by:
Hi,
I need to enable shadow copying network share folder assemblies. Currently
if the path is given as the network share folder path, shadow copying is not
enabled. Should something special be...
|
by: TB |
last post by:
Hi All:
This news group is proving to be great help on my path towards
mastering ASP.NET thanks to all of you helpful souls out there. I am
looking forward to the day when I can contribute...
|
by: Martin Ho |
last post by:
I am running into one really big problem.
I wrote a script in vb.net to make a copy of folders and subfolder to
another destination:
- in 'from.txt' I specify which folders to copy
- in...
|
by: Jerad Rose |
last post by:
I have a fairly simple C# console app which copies files from a network
folder to a local folder. When the app resides on my local C: drive, it
runs just fine. However, when the app resides on a...
|
by: DudDav |
last post by:
Hi All,
I have been trying to configured WebDAV on our current IIS server as an alternative to FTP and have had some mixed success but I've hit a brick wall with one problem.
The server...
|
by: writeson |
last post by:
Hi all,
I'm writing some code that monitors a directory for the appearance of
files from a workflow. When those files appear I write a command file
to a device that tells the device how to...
|
by: maheshbhalerao82 |
last post by:
Hi,
I am trying to copy a database from one server to another.
The folder structure of the another server is different than that of the first one.
so if I take the back up of the database and...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |