0

I've to do a windows service which starts before the user logon. It's also mean that this service has to do have its own username/password. So how can I open a network share remotly without being logged?

I'm in a windows domain, and the goal is to use some configured credentials to access the remote files I need.

Do you have an idea? I just can't find anything to access easily to the server

Edit: I'm sorry I made a mistake, the client computer isn't in the domain, the server is :(

J4N
  • 19,480
  • 39
  • 187
  • 340
  • See this answer: http://stackoverflow.com/questions/5433570/access-a-remote-directory-from-c/5433640#5433640 to learn how to authenticate on a remote share. – Daniel Hilgarth Aug 10 '11 at 13:39
  • Did you install the service with network dependencies? Is the username/password that you are using a domain account? If the answer is yes to both of these, you should be able to access the network share using unc path. – Alex Mendez Aug 10 '11 at 13:56

2 Answers2

4

Generally what you would do is set the service to run under a named user (domain) account and give that user access to the remote share. Then access the files on that share as you would any other i.e.

File.ReadAllText(@"\\SomeMachine\SomeShare\Something.txt");
iamkrillin
  • 6,798
  • 1
  • 24
  • 51
  • In fact I made a mistake, the computer on which the service is executed isn't a domain member, but the server on which is the share is. So I unfortunately can't use the user of the service, I've to impersonate it. – J4N Aug 10 '11 at 14:35
  • Another option would be to add a user account to the machine with the shares on it (a non domain user account) - Give them access to the share, add another user to the machine with the service running (same username and password). Then set the service to run under that user account. If you do that the authentication to the share should just "work" – iamkrillin Aug 10 '11 at 16:12
1

If you can't run the service as the user in question, you can do it through code with P/Invokes.

Connect to network drive with user name and password

Community
  • 1
  • 1
Joe
  • 41,484
  • 20
  • 104
  • 125