2

I have developed a windows service using local system as Account. I have used network path of file

FileInfo fi = new FileInfo(@"\\epm-server\penDocuments_LabMeds\" + Convert.ToString(dr["mrn"]) + "\\SyncedXML\\" + Convert.ToString(dr["xmlfile"]));
if (!fi.Exists)
    boolFileNotFound = true;

A dynamic path of a file that is built from database.

It works fine when I run Windows Service in Debug Mode, but when I install it then fileNotExists returns TRUE always like the file doesnt exist but infact it does exist.

This is bugging me a lot now. Kindly help me why its not working. Its a server path. Its getting opened in my PC.

Thanks

Martin
  • 16,093
  • 1
  • 29
  • 48
vickyshazad
  • 107
  • 1
  • 11
  • It works fine if I use it in a website. But its not working in a windows service. – vickyshazad Jun 28 '15 at 07:27
  • So permissions then as @MitchWheat already said... Try reading this: http://stackoverflow.com/questions/265953/c-how-can-you-easily-check-if-access-is-denied-for-a-file and also: http://stackoverflow.com/questions/458363/determining-if-file-exists-using-c-sharp-and-resolving-unc-path – Martin Jun 28 '15 at 07:30
  • I can access the path from my computer, So what could be the cause? On network what credentials do I need to use? Its a publically shared folder on server. – vickyshazad Jun 28 '15 at 07:34
  • @vickyshazad As already indicated, this is likely to be a permissions problem. The user that your service is running as does not have access to the file you are attempting to access. Either grant permission to you service user account, or use impersonation to access the file. – Martin Jun 28 '15 at 10:01

1 Answers1

3

Did you notice the double backslashes in front and after SyncedXML (\\SyncedXML\\)? This is probably the cause of your error.

Additionally I'd use string.Format in such cases to reduce the inadvertently addition of unwanted characters:

var path = string.Format(@"\\epm-server\penDocuments_LabMeds\{0}\SyncedXML\{1}", dr[mrn], dr[xmlfile]);
var fi = new FileInfo(path);

Edit:

If it's permission-related, then it's very likely that your local system account (in whose context the service is running) isn't allowed to access the epm-server.

The path is accessible if you're opening it directly or if you're running the service in debug mode as this is happening in your user context (e.g. YOURDOMAIN\vickyshazad), and you're allowed to access the ressource, whereas NT AUTHORITY\SYSTEM is not.

It's usually a good practise to have a special service account for your developed windows service and grant this user only and exactly the required permissions (least privilege). Maybe ask your system administrator for a service user.

Local System (NT AUTHORITY\SYSTEM) is a highly privileged account that's not recommended to use in general (see MSDN).

Most services do not need such a high privilege level. If your service does not need these privileges, and it is not an interactive service, consider using the LocalService account or the NetworkService account.

Jan Köhler
  • 5,817
  • 5
  • 26
  • 35
  • It didn't work either. Yes I can use string.Format. Thanks for that suggestion. Actually problem is with permission. – vickyshazad Jun 28 '15 at 09:52