472,146 Members | 1,283 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,146 software developers and data experts.

Copying Local Profile to network drive in .NET

Hello,

I am trying to find a way to create a scheduled task or
service that will copy my local profile folders
under "Documents and settings" to a network drive. This
would allow me to restore my settings if my profile became
tampered with or corrupt. Is there any sample code
available out there?

-Robert
Jul 19 '05 #1
3 3429
Thank you for getting me started down the path of the
creating a service to accomplish this. I created the
service with the code you provided me. However, I do have
few more challenges which I could use some more help.

The first challenge, is that I need a way to debug the
service before I deploy it, if this is possible.

The second challenge is that I need to copy the entire
directory underneath "Documents and Settings\rtaranti" for
example to my back up drive (Ex. h:\profiles) I hace
attempted to use the following code:

'************************************************* *
FSO = CreateObject("Scripting.FileSystemObject")
If (FSO.FolderExists(sDestinationFolderPath)) Then
FSO.CopyFolder(sSourceFolderPath, sDestinationFolderPath)
End If
'************************************************* **

My problem here is that some files in the subfolders, such
as the user.dat is locaked and cannot be copied. This
prevents the FileScripting object fomr continuing it's
job.
I noticed on my windows 2000 server machine, I have the
ability to perform the "CopyTo" method through
the "UserProfiles" tab under "My Computer/properites" to
copy the profile that I am logged in as to another
profile. This action seems to able to copy all the files
even though they are in session. Is there a way to re-use
code or objects like that?...Or someway to copy all the
files without having the process fail?

Thanks for all your help!

-Robert
-----Original Message-----
Hi Robert,

Here I write a sample for you. Hope this will help you.

using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
private static bool flag;
protected override void OnStart(string[] args) {
Thread wd = new Thread(new ThreadStart(RunThread)); flag=true;
wd.Start();
}
/// Stop this service.
protected override void OnStop()
{
flag = false;
allDone.WaitOne();
}
private static void RunThread()
{
while(flag)
{
try
{
File.Copy (@"c:\a.txt",@"\\fileserver\fsaf\b.txt",true); // thefileserver is where your network drive lies
}
catch(Exception e)
{ // to log the exception. StreamWriter sr = File.AppendText(@"C:\service.log"); sr.WriteLine (e.StackTrace.ToString()); sr.Flush();
sr.Close();
}
finally
{
}
}
allDone.Set();
}

Here is a link about how to write a windows service.
Please have a try and let me if this does the job for you.
Cheers!

--------------------
Content-Class: urn:content-classes:message
From: "Robert Tarantino" <rt******@promega.com>
Sender: "Robert Tarantino" <rt******@promega.com>
Subject: Copying Local Profile to network drive in .NET
Date: Mon, 18 Aug 2003 14:52:14 -0700
Lines: 10
Message-ID: <0b****************************@phx.gbl>
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
X-MIMEOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Thread-Index: AcNl0vxGha0FcRubQmyplraTC0riuw==
Newsgroups: microsoft.public.dotnet.general
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.general:104941NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
X-Tomcat-NG: microsoft.public.dotnet.general

Hello,

I am trying to find a way to create a scheduled task or
service that will copy my local profile folders
under "Documents and settings" to a network drive. This
would allow me to restore my settings if my profile becametampered with or corrupt. Is there any sample code
available out there?

-Robert
Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and

confers no rights.
.

Jul 19 '05 #2
Peter, in reference to the "Copy To" method". Could you
tell what windows object and function call is used to
perform this task? Is there any documentation on using
this outside of the windows interface?

-Robert

-----Original Message-----
Hi Robert,

1. My problem here is that some files in the subfolders, such as the user.dat is locaked and cannot be copied. This
prevents the FileScripting object fomr continuing it's
job.
You cannot touch the ntuser.dat file in the current account. When you login, the ntuser.dat hive from the appropriate profile is loaded into the registry as the HKEY_CURRENT_USER branch. You cannot even open the file programmatically for reading, because the OS locks the file for exclusive use. You have to login as another user (with admin rights) to access the file.

2. I noticed on my windows 2000 server machine, I have the ability to perform the "CopyTo" method through
the "UserProfiles" tab under "My Computer/properites" to
copy the profile that I am logged in as to another
profile. This action seems to able to copy all the files
even though they are in session. Is there a way to re- use code or objects like that?...Or someway to copy all the
files without having the process fail?

I have made a test and found that the ¡°CopyTo¡± method you refer to can not be applied to the current logon user account.

I suggest you do it in another account with admin rights [You may add the account to the administrators group]

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and
confers no rights.
--------------------
Content-Class: urn:content-classes:message
From: "Robert Tarantino" <rt******@promega.com>
Sender: "Robert Tarantino" <rt******@promega.com>
References: <0b****************************@phx.gbl>

<ZR**************@cpmsftngxa06.phx.gbl>
Subject: RE: Copying Local Profile to network drive in .NETDate: Wed, 20 Aug 2003 10:32:05 -0700
Lines: 140
Message-ID: <01****************************@phx.gbl>
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
Thread-Index: AcNnQPmZVnNP589QSDGwdm3LOuLgfA==
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Newsgroups: microsoft.public.dotnet.general
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.general:105218NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
X-Tomcat-NG: microsoft.public.dotnet.general

Thank you for getting me started down the path of the
creating a service to accomplish this. I created the
service with the code you provided me. However, I do have few more challenges which I could use some more help.

The first challenge, is that I need a way to debug the
service before I deploy it, if this is possible.

The second challenge is that I need to copy the entire
directory underneath "Documents and Settings\rtaranti" for example to my back up drive (Ex. h:\profiles) I hace
attempted to use the following code:

'*********************************************** ***
FSO = CreateObject("Scripting.FileSystemObject")
If (FSO.FolderExists(sDestinationFolderPath)) Then
FSO.CopyFolder(sSourceFolderPath, sDestinationFolderPath)End If
'*********************************************** ****

My problem here is that some files in the subfolders, such as the user.dat is locaked and cannot be copied. This
prevents the FileScripting object fomr continuing it's
job.
I noticed on my windows 2000 server machine, I have the
ability to perform the "CopyTo" method through
the "UserProfiles" tab under "My Computer/properites" to
copy the profile that I am logged in as to another
profile. This action seems to able to copy all the files even though they are in session. Is there a way to re- use code or objects like that?...Or someway to copy all the
files without having the process fail?

Thanks for all your help!

-Robert
-----Original Message-----
Hi Robert,

Here I write a sample for you. Hope this will help you.

using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
private static bool flag;
protected override void OnStart(string[]

args)
{
Thread wd = new Thread(new

ThreadStart(RunThread));
flag=true;
wd.Start();
}
/// Stop this service.
protected override void OnStop()
{
flag = false;
allDone.WaitOne();
}
private static void RunThread()
{
while(flag)
{
try
{
File.Copy

(@"c:\a.txt",@"\\fileserver\fsaf\b.txt",true); // the
fileserver is where your network drive lies
}
catch(Exception e)
{ // to log the

exception.
StreamWriter sr =

File.AppendText(@"C:\service.log");
sr.WriteLine

(e.StackTrace.ToString());
sr.Flush();
sr.Close();
}
finally
{
}
}
allDone.Set();
}

Here is a link about how to write a windows service.
Please have a try and let me if this does the job for you.Cheers!

--------------------
Content-Class: urn:content-classes:message
From: "Robert Tarantino" <rt******@promega.com>
Sender: "Robert Tarantino" <rt******@promega.com>
Subject: Copying Local Profile to network drive in .NET
Date: Mon, 18 Aug 2003 14:52:14 -0700
Lines: 10
Message-ID: <0b****************************@phx.gbl>
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
X-MIMEOLE: Produced By Microsoft MimeOLE V5.50.4910.0300Thread-Index: AcNl0vxGha0FcRubQmyplraTC0riuw==
Newsgroups: microsoft.public.dotnet.general
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl

microsoft.public.dotnet.general:104941
NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
X-Tomcat-NG: microsoft.public.dotnet.general

Hello,

I am trying to find a way to create a scheduled task or service that will copy my local profile folders
under "Documents and settings" to a network drive. This would allow me to restore my settings if my profile

became
tampered with or corrupt. Is there any sample code
available out there?

-Robert

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and

confers no rights.

.


.

Jul 19 '05 #3
Hi Robert,

I am glad that I can help you.
Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
Content-Class: urn:content-classes:message
From: "Robert Tarantino" <rt******@promega.com>
Sender: "Robert Tarantino" <rt******@promega.com>
References: <0b****************************@phx.gbl> <ZR**************@cpmsftngxa06.phx.gbl>
<01****************************@phx.gbl>
<DY**************@cpmsftngxa06.phx.gbl>
<03****************************@phx.gbl>
<Ff**************@cpmsftngxa06.phx.gbl>Subject: RE: Copying Local Profile to network drive in .NET
Date: Mon, 25 Aug 2003 07:34:38 -0700
Lines: 298
Message-ID: <0a****************************@phx.gbl>
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
X-Newsreader: Microsoft CDO for Windows 2000
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Thread-Index: AcNrFgMbUOY4iS9QTtygk6E092RpbA==
Newsgroups: microsoft.public.dotnet.general
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.general:105966
NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
X-Tomcat-NG: microsoft.public.dotnet.general

Thank you Peter. I think this is what I was looking for.
I really appreciate your efforts!
-Robert
-----Original Message-----
Hi Robert,

I think you may need to do it yourself.
There is a useful function SHGetFolderPath which will

retrieve the profile
related path.
http://support.microsoft.com/?id=252652
You may take a look at the link below.
http://support.microsoft.com/default.aspx?scid=kb;en-

us;310294

Hope this will help you.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and

confers no rights.
--------------------
Content-Class: urn:content-classes:message
From: "Robert Tarantino" <rt******@promega.com>
Sender: "Robert Tarantino" <rt******@promega.com>
References: <0b****************************@phx.gbl>

<ZR**************@cpmsftngxa06.phx.gbl>
<01****************************@phx.gbl>
<DY**************@cpmsftngxa06.phx.gbl>
Subject: RE: Copying Local Profile to network drivein .NETDate: Fri, 22 Aug 2003 16:28:00 -0700
Lines: 232
Message-ID: <03****************************@phx.gbl>
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
X-Newsreader: Microsoft CDO for Windows 2000
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Thread-Index: AcNpBQbx51d76UKbQx+zcai9OVCbLw==
Newsgroups: microsoft.public.dotnet.general
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gblmicrosoft.public.dotnet.general:105707NNTP-Posting-Host: TK2MSFTNGXA14 10.40.1.166
X-Tomcat-NG: microsoft.public.dotnet.general

Peter, in reference to the "Copy To" method". Could you
tell what windows object and function call is used to
perform this task? Is there any documentation on using
this outside of the windows interface?
-Robert
-----Original Message-----
Hi Robert,

1. My problem here is that some files in thesubfolders,such
as the user.dat is locaked and cannot be copied. This
prevents the FileScripting object fomr continuing it's
job.
You cannot touch the ntuser.dat file in the current
account. When you
login, the ntuser.dat hive from the appropriate profile
is loaded into the
registry as the HKEY_CURRENT_USER branch. You cannotevenopen the file
programmatically for reading, because the OS locks the
file for exclusive
use. You have to login as another user (with admin
rights) to access the
file.

2. I noticed on my windows 2000 server machine, I have
the
ability to perform the "CopyTo" method through
the "UserProfiles" tab under "My Computer/properites"tocopy the profile that I am logged in as to another
profile. This action seems to able to copy all thefileseven though they are in session. Is there a way to re-
use
code or objects like that?...Or someway to copy all the
files without having the process fail?

I have made a test and found that the ¡°CopyTo¡± method
you refer to can
not be applied to the current logon user account.

I suggest you do it in another account with admin rights
[You may add the
account to the administrators group]

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and
confers no rights.
--------------------
>Content-Class: urn:content-classes:message
>From: "Robert Tarantino" <rt******@promega.com>
>Sender: "Robert Tarantino" <rt******@promega.com>
>References: <0b****************************@phx.gbl>
<ZR**************@cpmsftngxa06.phx.gbl>
>Subject: RE: Copying Local Profile to network drive
in .NET
>Date: Wed, 20 Aug 2003 10:32:05 -0700
>Lines: 140
>Message-ID: <01****************************@phx.gbl>
>MIME-Version: 1.0
>Content-Type: text/plain;
> charset="iso-8859-1"
>Content-Transfer-Encoding: 7bit
>X-Newsreader: Microsoft CDO for Windows 2000
>Thread-Index: AcNnQPmZVnNP589QSDGwdm3LOuLgfA==
>X-MimeOLE: Produced By Microsoft MimeOLEV5.50.4910.0300>Newsgroups: microsoft.public.dotnet.general
>Path: cpmsftngxa06.phx.gbl
>Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.general:105218
>NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
>X-Tomcat-NG: microsoft.public.dotnet.general
>
>Thank you for getting me started down the path of the
>creating a service to accomplish this. I created the
>service with the code you provided me. However, I do
have
>few more challenges which I could use some more help.
>
>The first challenge, is that I need a way to debug the
>service before I deploy it, if this is possible.
>
>The second challenge is that I need to copy the entire
>directory underneath "Documents and Settings\rtaranti"
for
>example to my back up drive (Ex. h:\profiles) I hace
>attempted to use the following code:
>
>'******************************************** ******
>FSO = CreateObject("Scripting.FileSystemObject")
>If (FSO.FolderExists(sDestinationFolderPath)) Then
> FSO.CopyFolder(sSourceFolderPath,
sDestinationFolderPath)
>End If
>'******************************************** *******
>
>My problem here is that some files in the subfolders,
such
>as the user.dat is locaked and cannot be copied. This
>prevents the FileScripting object fomr continuing it's
>job.
>
>
>I noticed on my windows 2000 server machine, I havethe>ability to perform the "CopyTo" method through
>the "UserProfiles" tab under "My Computer/properites"to>copy the profile that I am logged in as to another
>profile. This action seems to able to copy all the
files
>even though they are in session. Is there a way to re-
use
>code or objects like that?...Or someway to copy allthe>files without having the process fail?
>
>Thanks for all your help!
>
>-Robert
>
>
>
>>-----Original Message-----
>>Hi Robert,
>>
>>Here I write a sample for you. Hope this will helpyou.>>
>>using System.IO;
>>using System.Threading;
>>using System.Runtime.InteropServices;
>> private static bool flag;
>> protected override void OnStart(string[]
>args)
>> {
>> Thread wd = new Thread(new
>ThreadStart(RunThread));
>> flag=true;
>> wd.Start();
>> }
>> /// Stop this service.
>> protected override void OnStop()
>> {
>> flag = false;
>> allDone.WaitOne();
>> }
>> private static void RunThread()
>> {
>> while(flag)
>> {
>> try
>> {
>> File.Copy
>(@"c:\a.txt",@"\\fileserver\fsaf\b.txt",true) ; // the
>>fileserver is where your network drive lies
>> }
>> catch(Exception e)
>> { // to log the
>exception.
>> StreamWriter sr =
>File.AppendText(@"C:\service.log");
>> sr.WriteLine
>(e.StackTrace.ToString());
>> sr.Flush();
>> sr.Close();
>> }
>> finally
>> {
>> }
>> }
>> allDone.Set();
>> }
>>
>>Here is a link about how to write a windows service.
>>Please have a try and let me if this does the job for
you.
>>Cheers!
>>
>>--------------------
>>>Content-Class: urn:content-classes:message
>>>From: "Robert Tarantino" <rt******@promega.com>
>>>Sender: "Robert Tarantino" <rt******@promega.com>
>>>Subject: Copying Local Profile to network drivein .NET>>>Date: Mon, 18 Aug 2003 14:52:14 -0700
>>>Lines: 10
>>>Message-ID: <0b****************************@phx.gbl>
>>>MIME-Version: 1.0
>>>Content-Type: text/plain;
>>> charset="iso-8859-1"
>>>Content-Transfer-Encoding: 7bit
>>>X-Newsreader: Microsoft CDO for Windows 2000
>>>X-MIMEOLE: Produced By Microsoft MimeOLE
V5.50.4910.0300
>>>Thread-Index: AcNl0vxGha0FcRubQmyplraTC0riuw==
>>>Newsgroups: microsoft.public.dotnet.general
>>>Path: cpmsftngxa06.phx.gbl
>>>Xref: cpmsftngxa06.phx.gbl
>microsoft.public.dotnet.general:104941
>>>NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
>>>X-Tomcat-NG: microsoft.public.dotnet.general
>>>
>>>Hello,
>>>
>>>I am trying to find a way to create a scheduled task
or
>>>service that will copy my local profile folders
>>>under "Documents and settings" to a network drive.
This
>>>would allow me to restore my settings if my profile
>became
>>>tampered with or corrupt. Is there any sample code
>>>available out there?
>>>
>>>-Robert
>>>
>>
>>
>>Regards,
>>Peter Huang
>>Microsoft Online Partner Support
>>Get Secure! www.microsoft.com/security
>>This posting is provided "as is" with no warrantiesand>confers no rights.
>>
>>.
>>
>

.

.


Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Robert Tarantino | last post: by
6 posts views Thread by Sue | last post: by
3 posts views Thread by Jeries Shahin | last post: by
2 posts views Thread by Jerad Rose | last post: by
reply views Thread by Saiars | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.