1

Can not find the file and the file if it is in that route.

FileInfo file = new FileInfo(@"\\10.125.16.22\Facturas Electronicas\Factura EMP000098.pdf");
if (file.Exists)
{
    EventLog.WriteEntry("encontro los adjuntos de la factura " + nrodocumento);
    File.Copy(ruta, @"C:\Factura\" + file.Name + ".pdf", true);
    cantidad++;
}
else
{
    EventLog.WriteEntry("no existe el adjunto " + ruta);
}

when it reaches if (file.Exists) the result is False. The type of project that I am using is Visual Studio Service Project.

the service windows in the configuration I have Account LocalSystem I must change it ?

if it is not "Local System" which should I handle?

  • 8
    Well, does the file exist? Does the user-account the program is running under have permission to read that share? – Dai Sep 05 '18 at 19:30
  • 3
    Buenas. It is a shared folder, usually windows services are started under system account. Have you tried changing it to a specific user credentials? – Cleptus Sep 05 '18 at 19:31
  • Possible duplicate of [Determining if file exists using c# and resolving UNC path](https://stackoverflow.com/questions/458363/determining-if-file-exists-using-c-sharp-and-resolving-unc-path) – JuanR Sep 05 '18 at 19:33
  • if the file exists but does not recognize it – MYA Technology Sep 05 '18 at 19:34
  • How do I give permission to this action? – MYA Technology Sep 05 '18 at 19:35
  • 2
    it also depends on which user the service starts with – csharpwinphonexaml Sep 05 '18 at 19:42
  • 1
    https://serverfault.com/questions/135867/how-to-grant-network-access-to-localsystem-account – Rufus L Sep 05 '18 at 19:50
  • @JuanR It's not a duplicate, the linked question involves similar code but doesn't mentions a service, that makes a huge difference in the outcome and the answer. – Alejandro Sep 05 '18 at 20:02
  • @Alejandro the linked question does not mention a service, but its the exact same proble. A window service by default does not run under a usser account that has rights to the shared resource so the root problem of the linked question and this question are the same. MYA must specify a user account to run the service that has rights on the shared resource. A domain user would be very easy. – Cleptus Sep 05 '18 at 21:18

1 Answers1

4

the service windows in the configuration I have Account LocalSystem I must change it ?

Yes, you must change this. The LocalSystem account won't have any permissions for the computer at 10.125.16.22. This is true even if it's the same computer! The UNC path will force a network access, and LocalSystem won't present any credentials over the network. Therefore File.Exists() will always return false, no matter the actual state of the file. This is covered at the end of the Remarks section of the documentation.

The Exists method returns false if any error occurs while trying to determine if the specified file exists. This can occur in situations that raise exceptions such as passing a file name with invalid characters or too many characters, a failing or missing disk, or if the caller does not have permission to read the file.

Additionally, it's almost always poor practice to use File.Exists() in the first place. Instead, just try to copy the file and handle the exception if it fails. You need to do this anyway, because there are lots of reasons a file copy might fail that have nothing to do with the file existing. This is also faster, because as slow as exception handling can be it's still usually much faster than the extra set of disk/network I/O operations invoked by the File.Exists() check.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • As a thumb rule I try not to answer duplicated questions, dito for the quality of the answer tho. – Cleptus Sep 05 '18 at 20:04
  • I go ahead and answer if it's a very new user. This guy has been around 9 months, but it's only his third question. Also, while this could be a duplicate of a generic File.Exists() question or a network access question, you could make a case that putting the two together is unique, or rare enough to answer. Finally, I tend to give some credit for the language barrier, since that would make searching for duplicates or adapting a generic duplicate to his specific situation more difficult. – Joel Coehoorn Sep 05 '18 at 20:12
  • if it is not "Local System" which should I handle? – MYA Technology Sep 05 '18 at 20:13
  • 1
    You need an actual user on the target system. This is easiest to accomplish in a domain environment, because the credentials must be accepted on _both_ systems. – Joel Coehoorn Sep 05 '18 at 20:14
  • The link in Rufus L's comment on the question will also be useful to you. – Joel Coehoorn Sep 05 '18 at 20:19