1
votes

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)

2
Do you have logs? What error message are you getting when the service tries to access the UNC?Raj More
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
You have to add this share to access files. In command line, you use net use, in Win32 API, WNetAddConnection3.Anton Tykhyy
@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# nowrojoca
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

2 Answers

1
votes

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

0
votes

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

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