Getting full path for Windows Service

后端 未结 7 1094
感情败类
感情败类 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: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.

提交回复
热议问题