[英]How i can get user desktop path from windows services?
This is my code, i need my service to make a shortcut to an application on user desktop if the shortcut not exist. 这是我的代码,如果快捷方式不存在,我需要我的服务为用户桌面上的应用程序创建快捷方式。 when i debug my application inside visual studio it will put the shortcut on my desktop, but when i run the service on windows it always will put the shortcut on C Drive not on my desktop.
当我在Visual Studio中调试我的应用程序时,它将快捷方式放在我的桌面上,但是当我在Windows上运行该服务时,它将始终将快捷方式放在C驱动器上而不在我的桌面上。
private void CreateShortcut()
{
object shDesktop = (object)"Desktop";
WshShell shell = new WshShell();
//string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string shortcutAddress = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\CadEisancy.lnk";
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress);
shortcut.Description = "New shortcut for a Notepad";
shortcut.Hotkey = "Ctrl+Shift+N";
shortcut.TargetPath = @"C:\RankWindowsApp\RankWindowsApp\bin\Debug\RankWindowsApp.exe";
shortcut.IconLocation = @"C:\RankWindowsApp\Icon.ico";
shortcut.Save();
}
protected override void OnStart(string[] args)
{
//Create Shortcut On Desktop
CreateShortcut();
}
My code: 我的代码:
![MyCode][1] ![MyCode] [1]
解决方案是获取当前记录的用户会话代码,然后根据会话获取其个人资料的名称,最后我们可以指定其桌面路径。
How your service is configured ? 您的服务如何配置? If you are not running under user account, the service will use its own profile as you see in your output .
如果您不在用户帐户下运行,那么该服务将使用自己的配置文件,如您在输出中看到的那样。
I have answered mostly similar answer in the following look. 在以下外观中,我已经回答了大多数类似的答案。 Please have a look if you feel that helps.
如果您觉得有帮助,请看看。
https://stackoverflow.com/a/46540108/1556780 https://stackoverflow.com/a/46540108/1556780
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.