Windows service - get current directory

后端 未结 4 430
庸人自扰
庸人自扰 2020-12-24 10:42

I have a Windows service that should look for a configuration file in its current directory.

So I use directory.getcurrentdirectiry() but instead of the

相关标签:
4条回答
  • 2020-12-24 10:48

    You can set the current directory to the directory that your service is running from by including this line in your code:

    System.IO.Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory);
    

    The important part of this is:

    System.AppDomain.CurrentDomain.BaseDirectory
    

    That returns the path to the directory your service is running from.

    0 讨论(0)
  • 2020-12-24 11:03

    Don't use Directory.GetCurrentDirectory(). I had the same exact problem with C:\Windows\System32 being returned. Use this instead:

    Path.GetDirectoryName(Application.ExecutablePath);

    0 讨论(0)
  • 2020-12-24 11:03

    Try this:

    System.Reflection.Assembly.GetEntryAssembly().Location
    
    0 讨论(0)
  • 2020-12-24 11:11

    Getting directory from full path:

    var location = System.Reflection.Assembly.GetEntryAssembly().Location;
    var directoryPath = Path.GetDirectoryName(location);
    

    Quite a silly problem when comparing to writing a Windows service :)

    0 讨论(0)
提交回复
热议问题