c# - 使用 C# 创建桌面快捷方式以添加主目录路径

标签 c# visual-studio-2015

这是我到目前为止写的。我在使用主目录路径创建桌面快捷方式时遇到问题。捕获路径并使用主目录路径在桌面上创建快捷方式链接的最佳方法是什么?非常感谢任何帮助,因为我是 C# 的初学者。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.DirectoryServices;
using System.Management;


namespace ConsoleApplication3
    {
    class Program
    {
    static void Main(string[] args)
    {

        String username = Environment.UserName;

        try
        {
            DirectoryEntry myLdapConnection = createDirectoryEntry();
            DirectorySearcher search = new DirectorySearcher(myLdapConnection);
            search.Filter = "(cn=" + username + ")";


            // add the objects to search for 

            string[] requiredProperties = new string[] {"homeDirectory"};

            foreach (String property in requiredProperties)
                search.PropertiesToLoad.Add(property);

            SearchResult result = search.FindOne();

            if (result != null)
            {
                foreach (String property in requiredProperties)
                    foreach (Object myCollection in result.Properties[property])
                        Console.WriteLine(String.Format("{0,-20} : {1}",
                                      property, myCollection.ToString()));
            }

            else Console.WriteLine("User not found!");
        }

        catch (Exception e)
        {
            Console.WriteLine("Exception caught:\n\n" + e.ToString());

        }
    }

    static DirectoryEntry createDirectoryEntry()
    {
            // create and return new LDAP connection 

            DirectoryEntry ldapConnection = new DirectoryEntry("Domain.com");
            ldapConnection.Path = "LDAP://OU=User,DC=test,DC=domain,DC=com";
            ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
            return ldapConnection;
        }
    }
}

最佳答案

您的问题可以分为两个问题:

<强>1。如何获取 homeDirectory 属性。

你可能已经有了这个,但我没有在你的代码片段中看到它,所以这里是如何获取 homeDirectory 属性:

使用您当前的逻辑,您可以将其添加到循环遍历 requiredPropertiesforeach 循环中:

if (property == "homeDirectory"){
    var homeDirectoryPath = result.Properties[property].ToString();

    // create desktop shortcut here 
}

或者如果你想直接从那个循环中得到它:

var homeDirectoryPath = result.Properties["homeDirectory"].ToString(); 

<强>2。走那条路,然后用它来创建桌面快捷方式。

This post详细介绍了如何创建桌面快捷方式。使用此代码并将 homeDirectory 路径放在正确的位置。它看起来是 TargetPath

您需要确保在使用时添加对 Windows 脚本宿主的 COM 引用:项目 > 添加引用 > COM > Windows 脚本宿主对象模型。

这是该帖子的代码:

using IWshRuntimeLibrary;


object shDesktop = (object)"Desktop";
WshShell shell = new WshShell();
string shortcutAddress = (string)shell.SpecialFolders.Item(ref shDesktop) + homeDirectoryPath;
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress);
shortcut.Description = "New shortcut for a Notepad";
shortcut.Hotkey = "Ctrl+Shift+N";
shortcut.TargetPath = Environment.GetFolderPath(Environment.SpecialFolders.System) + homeDirectoryPath;
shortcut.Save();

关于c# - 使用 C# 创建桌面快捷方式以添加主目录路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46551033/

相关文章:

C# 泛型约束 : Interface

visual-studio-2015 - 在 Visual Studio 2015 中为 ASP.NET 5 项目禁用自动添加新文件

visual-studio-2015 - azure logic app,visual studio,从门户打开现有的逻辑应用程序?

c# - 发布网站项目,以便它创建 .dll 而不是 .aspx.cs 文件

tfs - 团队基础服务器 : HTTP code 400 - Bad Request

c# - Exchange - Microsoft.Exchange.WebServices.Data.Recurrence.YearlyPattern 中缺少间隔

c# - 如何在 C# .NET 中为子类设置特定属性?

c# 将表单添加到项目: Possible to make it internal/private from designer?

c++ - OpenCL Hello World

c# - 在 SQL Server 数据库 C# 中保存表情