简体   繁体   English

我如何从Windows服务获取用户桌面路径?

[英]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.

相关问题 如何以编程方式访问远程桌面服务用户配置文件路径? - How do i access the remote desktop services user profile path programatically? 如何在Windows Phone中获取用户的日期? - how can I get a date from the user in Windows Phone? 如何获取用户的桌面完整地址? - how can i get the desktop full address of user? 如何在Windows中获得当前用户? - How can I get current user in windows? 如何从UWP获取Windows的用户友好版本名称,如“Windows 10 Pro” - How can I get User Friendly version name of Windows like 'Windows 10 Pro' from UWP 如何获取 C# 中当前用户的桌面路径? - How to get a path to the desktop for current user in C#? 如何在C#中获取常用桌面和开始菜单目录的路径? - How can i get the path to the common Desktop and Start Menu dirs in C#? 如何从控制台用户输入获取文本文件路径并在数组中使用它? - How can I get text file path from console user input and and use it in an array? 我怎样才能从另一个获取user.config的路径? - How can I get path of user.config from another assambly? 如何在特定时间从服务总线获取消息? - How can I get messages from Services Bus in a specific time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM