Move a file accross a network in C# using different credentials

Hi all,

I have a windows service that is polling a local folder for a particular criteria of file, when it finds it, it should then move the file to a network share on another computer.

The local machine account (which the service runs under) is not able to access that share, nor can the service be run by another account, it must be run under the local machine account.

I want to be able to specify domain user credentials for the service to be able to move the file to the network share.

Can anyone help?

Thanks,

Xavier.
LVL 1
locdangAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Bob HoffmanConnect With a Mentor DeveloperCommented:
Use Impersonation, The attached module has the code, the code snipet below shows how to use it, pretty simple.
You can covert the vb to C# here:
http://www.developerfusion.com/tools/convert/vb-to-csharp/ 

            'Impersonate an admin user
            If Impersonate("UserId", "Password", Domain") Then
                
                 'Do what you need to do a Admin here
 
                Call UnImpersonate()
            End If

Open in new window

Impersonation.txt
0
 
nhenny2009Connect With a Mentor Commented:
If you want to run the service as a domain account, then you will need to edit the local security policy (secpol.msc).  Make sure the domain user has the following rights

Log on as a service
Allow logon locally
Log on as batch

All of these services may not be necessary, and you can trim down and retest.
0
 
locdangAuthor Commented:
HBHoffman,
I am afraid that won't work, the machine the service is running on is not on the same domain as the destination share.

nhenny2009,
As I said in the question I cannot change the account the service runs as, it as to be the local machine account.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Bob HoffmanConnect With a Mentor DeveloperCommented:
Can you map a drive (on the machine running the service) to the share and have the service connect to the mapped drive?
0
 
locdangAuthor Commented:
A mapped drive wouldn't work as its a service. It needs to be able to run when no one is logged in.
0
 
nhenny2009Commented:
Sorry, I misunderstood.
0
 
nhenny2009Connect With a Mentor Commented:
So I assume you also could not run the service and network service account?
0
 
Bob HoffmanConnect With a Mentor DeveloperCommented:
You may have already considered a helper service, but if not:
http://stackoverflow.com/questions/182750/how-to-map-a-network-drive-to-be-used-by-a-service 
0
 
Anurag ThakurTechnical ManagerCommented:
i have a question regarding the following statement

>> The local machine account (which the service runs under) is not able to access that share, nor can the service be run by another account, it must be run under the local machine account.

why the service cannot be run under any other domain authenticated account other then local machine account
are there any limitations
0
All Courses

From novice to tech pro — start learning today.