0

I get an error of

> Application: RfidService.exe Framework Version: v4.0.30319
> Description: The process was terminated due to an unhandled exception.
> Exception Info: System.IO.DirectoryNotFoundException    at
> System.IO.__Error.WinIOError(Int32, System.String)    at
> System.IO.FileStream.Init(System.String, System.IO.FileMode,
> System.IO.FileAccess, Int32, Boolean, System.IO.FileShare, Int32,
> System.IO.FileOptions, SECURITY_ATTRIBUTES, System.String, Boolean,
> Boolean, Boolean)    at System.IO.FileStream..ctor(System.String,
> System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, Int32,
> System.IO.FileOptions, System.String, Boolean, Boolean, Boolean)

This is in the event viewer when I start my service application. It can be installed and it starts but in the event viewer it gives me the error mentioned above so it doesn't work. The only file that is needed is in the bin already and this is how I invoke it in the code.

using (var streamReader = new StreamReader(Directory.GetCurrentDirectory() + $@"\JsonData\ApplicationSetting.json"))
{
    var jsonString = streamReader.ReadToEnd();
    _applicationSetting = JsonConvert.DeserializeObject<ApplicationSetting>(jsonString);

    if (_applicationSetting != null)
    {
        _pcsConnectionString =
            $@"Data Source={_applicationSetting.PcsSetting.DataSource};Initial Catalog={_applicationSetting.PcsSetting.InitialCatalog};user={_applicationSetting.PcsSetting.User};Password={_applicationSetting.PcsSetting.Password}";
        _lcsConnectionString =
            $@"Data Source={_applicationSetting.LaneSetting.DataSource};Initial Catalog={_applicationSetting.LaneSetting.InitialCatalog};user={_applicationSetting.LaneSetting.User};Password={_applicationSetting.LaneSetting.Password}";
        _serviceConnectionString =
            $@"Data Source={_applicationSetting.ServiceDatabase.DataSource};Initial Catalog={_applicationSetting.ServiceDatabase.InitialCatalog};user={_applicationSetting.ServiceDatabase.User};Password={_applicationSetting.ServiceDatabase.Password}";
        _sendingDelay = _applicationSetting.LcsSetting.SendingDelay;
        _activeQueueTimeInterval = _applicationSetting.LcsSetting.ActiveQueueTimeInterval;
        _messageLoggerEngine = new MessageLoggerEngine(_serviceConnectionString);
    }
}

What can I try to resolve this?

Edit

The application exe and the folder where the json file resides in the same directory. See this image:

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
Ibanez1408
  • 4,550
  • 10
  • 59
  • 110
  • 1
    The exception is coming from this line: `var streamReader = new StreamReader(Directory.GetCurrentDirectory() + $@"\JsonData\ApplicationSetting.json")`, verify that this is a valid path. – Ryan Wilson Jan 07 '21 at 04:29
  • 1
    `GetCurrentDirectory` is the current working directory of the app, which is *not* necessarily the same as the directory of your `.exe`. – dxiv Jan 07 '21 at 04:32
  • @RyanWilson, when I run my console application, it runs fine. It can find the directory and the file. It's just when I run it as a service, it gives me that error. – Ibanez1408 Jan 07 '21 at 04:52
  • @dxiv Please see my Edit. Thank you. – Ibanez1408 Jan 07 '21 at 04:55
  • 1
    @Ibanez1408 log the path returned by `Directory.GetCurrentDirectory` when running it as a service and check to see if it's returning what you think it is – Ryan Wilson Jan 07 '21 at 05:05
  • Try this `System.AppDomain.CurrentDomain.BaseDirectory` instead of `Directory.GetCurrentDirectory` – TheGeneral Jan 07 '21 at 05:07
  • @Ibanez1408 See [Windows service - get current directory](https://stackoverflow.com/questions/10376253/windows-service-get-current-directory). – dxiv Jan 07 '21 at 05:33
  • @RyanWilson I get this value "D:\RfidService\bin\Debug\JsonData\ApplicationSetting.json" which is actually correct. – Ibanez1408 Jan 07 '21 at 05:59

1 Answers1

-1

You can use

using (var streamReader = new System.IO.StreamReader(Directory.GetCurrentDirectory() + $@"\JsonData\ApplicationSetting.json"))

Asad
  • 617
  • 2
  • 8
  • 23