Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

c# - Windows service gives error System.IO.DirectoryNotFoundException

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() + $@"JsonDataApplicationSetting.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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...