查找终端服务用户的文档文件夹
问题描述:
我试图让用户通过包含浏览器插件的IIS aspx页面使用终端服务器的本地扫描仪。该插件可以扫描文件并将数据传递到aspx页面,该页面将文件上传到服务器。查找终端服务用户的文档文件夹
我正在使用HttpContext.Current.User.Identity.Name来获取包含远程用户的名称的字符串。我如何在终端服务器上找到用户的文档文件夹?
答
取得了一些进展。这里有一个方法找到一个已知用户名一个路径使用PowerShell:
$user = Get-WmiObject Win32_UserAccount | Where-Object { $_.Name -eq 'known_username' }
cd 'HKLM:\software\Microsoft\Windows NT\CurrentVersion\ProfileList'
$key = Get-Item $user.SID
$saveLocation = $key.GetValue("ProfileImagePath")
这里的C#版本:
string loginName = HttpContext.Current.User.Identity.Name;
Regex r = new Regex(@"\w+[\\]");
string userName = r.Replace(loginName, "");
throw new System.ArgumentException("Debug: '" + HttpContext.Current.User.Identity.IsAuthenticated.ToString() +"'", "userName");
SelectQuery sQuery = new SelectQuery("Win32_UserAccount", "name='" + userName + "'");
ManagementObjectSearcher mSearcher = new ManagementObjectSearcher(sQuery);
string sid = "";
foreach (ManagementObject mObject in mSearcher.Get())
{
sid = mObject["SID"].ToString();
break; //mSearcher.Get() is not indexable. Behold the hackyness!
}
string saveLocation = Microsoft.Win32.Registry.GetValue(
"HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\" + sid,
"ProfileImagePath", null).ToString();
C#是不是我的母语;有可能完成这个的方法不那么直率。但它确实工作。