1

I have a C# .NET service that runs as DOMAIN\username on a windows server and it needs to access a network share \\share\export docs$\.

If I'm logged into the machine (as DOMAIN\username) I can see the share and I have even mapped it to X: and tried to use that. I set the path in app.config (and restart the service) and if I use a path on C: it works fine but if I try using \\share\export docs$\ or X:\ it can't see any of the files I need to access.

I have tried running the service as both a local user and as DOMAIN\username

Any ideas what I might be doing wrong?

EDIT: Stacktrace from log:

Could not find a part of the path 'X:\200909\11324709\coo.pdf'.
    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
    at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
    at System.Net.Mail.AttachmentBase.SetContentFromFile(String fileName, String mediaType)

at System.Net.Mail.Attachment..ctor(String fileName)

rojoca
  • 11,040
  • 4
  • 45
  • 46
  • Do you have logs? What error message are you getting when the service tries to access the UNC? – Raj More Sep 21 '09 at 20:11
  • Check the permissions on the files in the share. It's possible the user can reach the share but not to its files. – David Andres Sep 21 '09 at 20:13
  • 1
    You have to add this share to access files. In command line, you use `net use`, in Win32 API, `WNetAddConnection3`. – Anton Tykhyy Sep 21 '09 at 20:17
  • @Raj: Added the stacktrace @David: I can read/write all files on the share as the user @Anton: Having a look at trying to do this in C# now – rojoca Sep 21 '09 at 21:04
  • UPDATE: So after going the WNetUseConnection route, copying and pasting from the net like crazy, and hacking it in just to see if it would work (which it does), I try it again with the UNC path and it works fine. I suspect someone at the IS department hates me. – rojoca Sep 21 '09 at 22:25

2 Answers2

1

Mapped drives in .Net are a bit buggy. However, this type of situation comes up in questions here quite a bit. You wold be better to specify the UNC path.

This isn't the exact same question you asked, but this post is relevant to your question and should help you out.

How do I access a file share programattically

Community
  • 1
  • 1
David
  • 72,686
  • 18
  • 132
  • 173
  • Looks promising, thanks. Should probably search a bit harder. – rojoca Sep 21 '09 at 20:26
  • Don't worry about searching harder. I mean it's a good idea, but don't kick yourself for not finding it. The question was phrased differently so it would have been hard to find. I just happened to have seen that one and it was fresh in my head. – David Sep 21 '09 at 20:37
0

Running your service as Domain\username "should" work.

Are you sure that account has "login as service" rights?

Scrappydog
  • 2,864
  • 1
  • 21
  • 23