Getting full path for Windows Service

后端 未结 7 1086
感情败类
感情败类 2021-01-31 01:22

How can I find out the folder where the windows service .exe file is installed dynamically?

Path.GetFullPath(relativePath);

returns a path base

相关标签:
7条回答
  • 2021-01-31 02:04

    Try

    System.Reflection.Assembly.GetEntryAssembly().Location
    
    0 讨论(0)
  • 2021-01-31 02:07

    Another version of the above:

    string path = Assembly.GetExecutingAssembly().Location;
    FileInfo fileInfo = new FileInfo(path);
    string dir = fileInfo.DirectoryName;
    
    0 讨论(0)
  • 2021-01-31 02:09

    This works for our windows service:

    //CommandLine without the first and last two characters
    //Path.GetDirectory seems to have some difficulties with these (special chars maybe?)
    string cmdLine = Environment.CommandLine.Remove(Environment.CommandLine.Length - 2, 2).Remove(0, 1);
    string workDir = Path.GetDirectoryName(cmdLine);  
    

    This should give you the absolute path of the executable.

    0 讨论(0)
  • 2021-01-31 02:14

    Environment.CurrentDirectory returns current directory where program is running. In case of windows service, returns %WINDIR%/system32 path that is where executable will run rather than where executable deployed.

    0 讨论(0)
  • 2021-01-31 02:15

    This should give you the path that the executable resides in:

    Environment.CurrentDirectory;
    

    If not, you could try:

    Directory.GetParent(Assembly.GetEntryAssembly().Location).FullName
    

    A more hacky, but functional way:

    Path.GetFullPath("a").TrimEnd('a')
    

    :)

    0 讨论(0)
  • 2021-01-31 02:18
    Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)
    
    0 讨论(0)
提交回复
热议问题