none
C# Windows Service start error 1064 Directory not found when changing working directory RRS feed

  • Question

  • Hi,

    I'm running into a little behavior that I cannot explain. I've build a C# Service that watches the Security EventLog for specific events and generates a little report based on them. 

    The application runs on a Windows 2008 R2 Standard Server with .NET 4.8 installed. The target framework is .NET 4.7.2 and the service runs as Local System. 

    So far the application works like a charm but... in the wrong folder and I seem unable to change it. The default folder is "C:\Windows\SysWow64\", normal for a 32-bits service. But I'd like to point it to another folder "C:\TestApps\EventLogGuard" as a working directory. 

    I've tried to change the working directory or path it writes files to in two ways (both resulting in the same error):

    1. By changing the working directory

    string path = @"C:\TestApps\EventLogGuard";
    Directory.SetCurrentDirectory(path);

    2. By changing the path I'm writing to in a streamwriter to:

    string fileName = @"C:\TestApps\EventLogGuard\IPList.txt"; string fileLog = @"C:\TestApps\EventLogGuard\EventLogGuard.log";

    using (System.IO.StreamWriter file = new System.IO.StreamWriter(fileName))
    {

    ~~~~~~

    }


    As soon as either option is added I'm getting an error 1064 on-start. In the Windows EventLog I can see the following error displayed:

    Problem signature:
    P1: EventLogGuardSVC.exe
    P2: 1.0.0.0
    P3: f8080359
    P4: mscorlib
    P5: 4.8.3761.0
    P6: 5c9c6e98
    P7: 168e
    P8: fc
    P9: System.IO.DirectoryNotFound
    P10: 

    Does anyone have an idea what may be causing this behavior or experienced something similar? 

    Edit: The directory exists and I do have a check added if the path exists as well, makes no difference.


    • Edited by Ray Zorg Friday, October 25, 2019 8:44 AM
    Friday, October 25, 2019 8:43 AM

Answers

  • A good thought is permission issues, see the following page on what is going on and suggestions to continue. Also to see if this is the right direction, alter your code to not have to change directories but instead access information by pointing to the path intended to work with, if you still get errors it's permissions which needs to be dealt with outside of code first.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, October 25, 2019 10:08 AM
    Moderator
  • Hi,

    Thank you for the feedback. I got the issue fixed in the end. It appeared to be a rights issue on the EventLogGuard folder. 

    Wednesday, November 6, 2019 7:55 AM

All replies

  • A good thought is permission issues, see the following page on what is going on and suggestions to continue. Also to see if this is the right direction, alter your code to not have to change directories but instead access information by pointing to the path intended to work with, if you still get errors it's permissions which needs to be dealt with outside of code first.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, October 25, 2019 10:08 AM
    Moderator
  • Hi Ray,

    Thank you for posting here.

    For your question, you seem to have encountered System.IO.DirectoryNotFound while switching the default folder.

    You only provided a part of the code, so I can’t reproduce your problem.

    So I suggest you can use Directory.Exists() to check if the folder exists, there may be incorrect characters in the string that are difficult to distinguish.

    If this doesn't help you, please provide more information or code so that we can reproduce your problem.

    Best regards,
    Timon

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 28, 2019 9:48 AM
  • Does that path actually exist? If a path doesn't exist fully then you first need to create it using `Directory.CreateDirectory`. So can you navigate, right now using File Explorer, to `C:\TestApps\EventLogGuard`? 

    Michael Taylor http://www.michaeltaylorp3.net

    Monday, October 28, 2019 2:04 PM
    Moderator
  • Hi,

    Thank you for the feedback. I got the issue fixed in the end. It appeared to be a rights issue on the EventLogGuard folder. 

    Wednesday, November 6, 2019 7:55 AM