how to get logged in user name from window service

后端 未结 2 732
醉话见心
醉话见心 2021-01-24 06:32

how to get logged in user name from window service. when i do the same thing using Environment.UserName i get username as \"System\".

thanks Arvind

2条回答
  •  迷失自我
    2021-01-24 07:11

    You get System because your service runs under the System account.

    There is an article on codeproject that discusses the same thing .. check it out http://www.codeproject.com/KB/vb/Windows_Service.aspx

    System.Diagnostics.Process[] objArrProcess = System.Diagnostics.Process.GetProcessesByName("explorer");
    string strCurrentUserName = objArrProcess[0].StartInfo.EnvironmentVariables["username"];
    

    This works for me.

提交回复
热议问题