Servicio de Windows-obtener directorio actual


Tengo un servicio de Windows que debería buscar un archivo de configuración en su directorio actual.

Así que uso directory.getcurrentdirectiry() pero en lugar del directorio de servicios obtengo

c:\windows\system32

¿Alguna idea de por qué y cómo debo obtener el directorio de servicios?

Author: marc_s, 2012-04-30

5 answers

No use Directory.GetCurrentDirectory(). Tuve exactamente el mismo problema con C:\Windows\System32 siendo devuelto. Usa esto en su lugar:

Path.GetDirectoryName(Application.ExecutablePath);

 6
Author: puffgroovy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-04-14 18:23:59

Puede establecer el directorio actual en el directorio desde el que se ejecuta su servicio incluyendo esta línea en su código:

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

La parte importante de esto es:

System.AppDomain.CurrentDomain.BaseDirectory

Que devuelve la ruta al directorio desde el que se está ejecutando el servicio.

 101
Author: Jed,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-06-03 22:26:12

Prueba esto:

System.Reflection.Assembly.GetEntryAssembly().Location
 27
Author: coder,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-04-29 21:43:50

Obteniendo el directorio desde la ruta completa:

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

Un problema bastante tonto cuando se compara con escribir un servicio de Windows:)

 16
Author: Michael Brennt,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-07-19 11:00:01

string applicationDir = AppDomain.CurrentDomain.BaseDirectory;

 3
Author: dsdel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-09-30 14:39:53