这是我到目前为止所写的内容。我在使用主目录路径创建桌面快捷方式时遇到问题。使用homedirectory路径捕获路径并在桌面上创建快捷方式链接的最佳方法是什么?我非常感谢任何帮助,因为我是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;
}
}
}
答案 0 :(得分:0)
您的问题可分为两个问题:
<强> 1。如何获取T foo
属性。
您可能已经拥有此功能,但我没有在您的代码段中看到它,因此以下是获取homeDirectory
属性的方法:
使用您拥有的当前逻辑,您可以将其添加到循环浏览homeDirectory
的{{1}}循环中:
foreach
或者如果你想直接从那个循环中获取它:
requiredProperties
<强> 2。走这条路并转而使用它来创建桌面快捷方式。
This post详细介绍了如何创建桌面快捷方式。使用此代码的代码并将if (property == "homeDirectory"){
var homeDirectoryPath = result.Properties[property].ToString();
// create desktop shortcut here
}
路径放在正确的位置。它看起来像var homeDirectoryPath = result.Properties["homeDirectory"].ToString();
。
使用此项时,您需要确保向Windows Scripting Host添加COM引用:Project&gt;添加参考&gt; COM&gt; Windows脚本宿主对象模型。
以下是该帖子的代码:
homeDirectory
答案 1 :(得分:0)
这是最终的代码。如果未映射主驱动器,则通过ping DC来检查用户是否在域中。如果用户在域中,则映射驱动器并将驱动器的快捷方式添加到用户的桌面。
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using System.IO;
using IWshRuntimeLibrary;
using System.Reflection;
using Shell32;
using System.Net.NetworkInformation;
using System.Threading;
using System.Management;
using System.Security.Principal;
if (!Directory.Exists(@"H:\"))
{
//ping Hdrive server
try
{ //find current domain controller
using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
{
string controller = context.ConnectedServer;
//ping current domain controller
Ping ping = new Ping();
PingReply pingReply = ping.Send(controller);
if (pingReply.Status == IPStatus.Success)
{
try
{
//get current username
String username = Environment.UserName;
//Lookup current username in AD
DirectoryEntry myLdapConnection = createDirectoryEntry();
DirectorySearcher search = new DirectorySearcher(myLdapConnection);
search.Filter = "(cn=" + username + ")";
//Search for User's Home Directory
string[] requiredProperties = new string[] { "homeDirectory" };
foreach (String property in requiredProperties)
search.PropertiesToLoad.Add(property);
SearchResult result = search.FindOne();
// If home directory info is not blank
if (result != null)
{
//pass the homedirectory path into a variable
string path = "";
foreach (String property in requiredProperties)
foreach (Object myCollection in result.Properties[property])
path = (myCollection.ToString());
//map Hdrive (non persistent map)
System.Diagnostics.Process.Start("net.exe", "use /persistent:NO H: " + path);
//create a desktop shortcut to Hdrive
var wsh = new IWshShell_Class();
IWshRuntimeLibrary.IWshShortcut shortcut = wsh.CreateShortcut(
Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\H Drive.lnk") as IWshRuntimeLibrary.IWshShortcut;
shortcut.TargetPath = path;
shortcut.Save();
}
}
catch (Exception)
{
//do nothing
}
}
}
}
catch (Exception)
{
//do nothing
}
}
}
public static DirectoryEntry createDirectoryEntry()
{
// create and return new LDAP connection
DirectoryEntry ldapConnection = new DirectoryEntry("mydomain.com");
ldapConnection.Path = "LDAP://mydomain.com/OU=Users,DC=mydomain,DC=Com";
ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
return ldapConnection;
}