2

I have setup a Console, Library, and Service project. The Library project loads up files in a folder and stores the text in variables.

The console project will then manipulate those variables. I am able to run the Console project successfully and it loads the files without a problem. The files are set to Copy Always in the DLL and are specified as Content for the Build Action.

When I try to run the exact same code in the service.

File.ReadAllText(@"MyFolder\SomeFile.txt");

It throws the following exception.

The type initializer for 'MyLibrary.Data.ReadFiles' threw an exception.

I am using the Setup Project type to create the installer. After the service installs the folder MyFolder does exist in the Application Folder of the service, as well as the files. What else could be causing my problem for the service not being able to read those files?

EDIT 1

public class ReadFiles {
    public static string DataFile = File.ReadAllText(@"MyFolder\SomeFile.txt");
}
Steven Combs
  • 1,890
  • 6
  • 29
  • 54

2 Answers2

1

The service account that you're running the Windows service under, doesn't have rights to read from the location you're trying to access. The error you're seeing has to do with the fact that the code you showed likely exists in the construction of the type ReadFiles.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • The service account is `LOCAL SYSTEM`. How do I allow permissions for it to read said folder? SYSTEM is granted everything on the folder as well as the files. – Steven Combs Jun 05 '13 at 17:40
  • I definitely agree, just trying to figure out the solution. – Steven Combs Jun 05 '13 at 17:41
  • @meanbunny, if you're running as `LOCAL SYSTEM` then you're simply throwing an exception in the constructor of `ReadFiles`. Post that code, or you may even be able to see it, and check the event viewer for an unhandled .NET exception which almost certainly exists. – Mike Perrenoud Jun 05 '13 at 17:42
  • The problem is because it is looking in C:\Windows\System32 – Steven Combs Jun 05 '13 at 18:02
  • @meanbunny, so it isn't finding the files. Apparently the construction of the `ReadFiles` class needs to get the path from somewhere else so that it can get the right path? – Mike Perrenoud Jun 05 '13 at 18:03
  • 2
    I think I just need to supply the full path. I have installed this thing with a Setup Project installer. So it is installing it into Program Files. I think Services by default want to automatically look in the system32 folder because of svchost.exe, since that is what the service is running under. – Steven Combs Jun 05 '13 at 18:05
  • @meanbunny, that is correct, and in fact also why generally services need to get their path information out of an `app.config` file rather than leveraging their currently executing directory. It is possible to recover the installation folder, but may be more flexible if leveraging the `app.config` file. – Mike Perrenoud Jun 05 '13 at 18:09
0

Was facing similar issue in one of the windows service for running node js application, for sending emails. It was running fine when we run from the same directory, but running same application as windows service was having issues and couldn't send emails. Comment from @meanbunny did help, so adding it as an answer.

Windows service can't access the directory/files if they are mentioned in the code as relative path, better use the absolute path or be more flexible as @Mike mentioned in his comment to use app.config for each service.

emarshah
  • 328
  • 3
  • 14