I have a network shared folder mapped to a drive letter, which is accessible from Windows Explorer, from the command prompt as well as from my WinForms application without problem. It is also accessible from my Windows service using a UNC path.
However, when I attempt to access this network location using a mapped drive letter from the Windows service, access fails. The Windows service is configured to use my personal "Log On" account credentials, which is the same in all the above cases. I am an administrator.
Many customer sites utilize drive letters for network shares and I cannot always control this and force them to specify UNC paths instead. I need to be able to access network shares using drive letters from a Windows service.
What do I need to do to set up my Windows service, so that it can access network shared folders that are mapped to drive letters? My Windows service is written in C#.
Sorry; you can't access mapped drives from Windows services. As Sheng suggested, you can use a UI process to get the UNC path from a mapped drive and then pass this to the service, which must use the UNC path.
mapped drives are per session objects. So each interactive session has its own mapping and the service session has another drive mapping. In order to get the correct UNC path of a mapped drive you need to call WNetGetConnection in the correct session.
You can use any inter-session communication methods to initiate the request and get the result in the service, such as WCF, named pipe, sockets, etc.
hi elan i faced the same problem in my project and i found a solution
and is work expected follow my steps
if (api.Docusign_download(strDocuSignUserName, strDocuSignPassword, strDocuSignIntegratorKey, EnvelopeID, Environment.ExpandEnvironmentVariables("%temp%")) == true)
{
if (m_streamWriter1 != null)
{
m_streamWriter1.WriteLine(" This envelop id is Downloaded and update the table" + EnvelopeID + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + "\n");
}
fpath1 = Environment.ExpandEnvironmentVariables("%temp%") + '\\' + EnvelopeID + '1' + ".pdf";
fpath2 = Environment.ExpandEnvironmentVariables("%temp%") + '\\' + EnvelopeID + '2' + ".pdf";
if (System.IO.File.Exists(fpath1))
{
fso = new FileSystemObject();
// fso.CopyFile(fileLoc, "\\\\Tech-Pro-01\\D\\", true); i download the file in temp folder and copy file to unc path ur expected work on reverse like access file to unc path he does not work directly but work in in direct access like temp folder to services
fso.CopyFile(fpath1, UNC, true);
fso.CopyFile(fpath2, UNC, true);
fso.DeleteFile(fpath1, true);
fso.DeleteFile(fpath2, true);
//System.IO.File.Move(fileLoc, fileLocMove);
im just using legacy application script in vb fso file system object
1,make sure your map path access in iuser and network service access enable to the mapped provided machine 2,adding the reference system scripting
3, and unc path example \computername\sharedname\folder\filename 4,just fso.copyfile(uncpath,tempfoler,true) 5,u access a your file in temp folder he is access expected and work perfect
the temp folder access "c:\windows\temp because proceess can take the windows temp folder only
hope u elan he is work perfectly
thanks and regards
jagadeesh Govindaraj Pillai jagadeesh1492@facebook.com
来源:https://stackoverflow.com/questions/3098486/cannot-access-files-on-drive-mapped-network-share-from-a-windows-service