Environment.SpecialFolder 和模拟
Environment.SpecialFolder and Impersonation
我用的是Visual Studio2013,Windows8.1.
我尝试从 模拟用户 获取 Environment.SpecialFolder
值,但我总是得到 空字符串值 .
有什么建议吗?
我的测试代码:
[TestMethod]
public void Environment_Folders_is_ok()
{
Console.WriteLine("CURRENT USER " + Environment.UserName);
GlobalVariables.MyDocumentsPath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Console.WriteLine(GlobalVariables.MyDocumentsPath);
var p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
Console.WriteLine(p1);
p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
Console.WriteLine(p1);
p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
Console.WriteLine(p1);
p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
Console.WriteLine(p1);
var user = new Utils().DecryptValue(ConfigurationManager.AppSettings["User"]);
var password = new Utils().DecryptValue(ConfigurationManager.AppSettings["Password"]);
var domain = new Utils().DecryptValue(ConfigurationManager.AppSettings["Domain"]);
GlobalVariables.Impersonate = true;
using (new Impersonator(user, domain, password))
{
Console.WriteLine("IMPERSONATE with " + user);
p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Console.WriteLine(p1);
p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
Console.WriteLine(p1);
p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
Console.WriteLine(p1);
p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
Console.WriteLine(p1);
}
}
输出:
CURRENT USER MyUser
C:\Users\MyUser\Documents
C:\Users\MyUser
C:\Users\MyUser\Desktop
C:\Users\MyUser\Desktop
IMPERSONATE with user2
更新:
此外,我使用 FolderBrowserDialog
和方法 ShowDialog
,但出现错误
Unable to retrieve the root folder
当我使用模拟用户时
try
{
Console.WriteLine("FolderBrowserDialog. IMPERSONATE WITH " + user);
using (var fbd = new FolderBrowserDialog())
{
Console.WriteLine(fbd.RootFolder);
p1 = System.Environment.GetFolderPath(fbd.RootFolder);
Console.WriteLine(p1);
DialogResult result = fbd.ShowDialog();
}
}
catch (System.InvalidOperationException ex)
{
Assert.AreEqual("Unable to retrieve the root folder.", ex.Message, "No se esperaba " + ex.Message);
}
输出:
FolderBrowserDialog. IMPERSONATE with user2
Desktop
我使用 LOGON32_LOGON_NEW_CREDENTIALS
(LogonType.LOGON32_LOGON_NEW_CREDENTIALS, LogonProvider.LOGON32_PROVIDER_WINNT50
) 模仿
https://platinumdogs.me/2008/10/30/net-c-impersonation-with-network-credentials/
和我得到的路径与用户 MyUser 相同。
我的代码:
using (new Impersonator(user, domain, password
, LogonType.LOGON32_LOGON_NEW_CREDENTIALS, LogonProvider.LOGON32_PROVIDER_WINNT50
))
{
Console.WriteLine("IMPERSONATE LOGON32_LOGON_NEW_CREDENTIALS with " + user);
p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Console.WriteLine(p1);
p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
Console.WriteLine(p1);
p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
Console.WriteLine(p1);
p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
Console.WriteLine(p1);
}
输出:
IMPERSONATE LOGON32_LOGON_NEW_CREDENTIALS with user2
C:\Users\MyUser\Documents
C:\Users\MyUser
C:\Users\MyUser\Desktop
C:\Users\MyUser\Desktop
我用的是Visual Studio2013,Windows8.1.
我尝试从 模拟用户 获取 Environment.SpecialFolder
值,但我总是得到 空字符串值 .
有什么建议吗?
我的测试代码:
[TestMethod]
public void Environment_Folders_is_ok()
{
Console.WriteLine("CURRENT USER " + Environment.UserName);
GlobalVariables.MyDocumentsPath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Console.WriteLine(GlobalVariables.MyDocumentsPath);
var p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
Console.WriteLine(p1);
p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
Console.WriteLine(p1);
p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
Console.WriteLine(p1);
p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
Console.WriteLine(p1);
var user = new Utils().DecryptValue(ConfigurationManager.AppSettings["User"]);
var password = new Utils().DecryptValue(ConfigurationManager.AppSettings["Password"]);
var domain = new Utils().DecryptValue(ConfigurationManager.AppSettings["Domain"]);
GlobalVariables.Impersonate = true;
using (new Impersonator(user, domain, password))
{
Console.WriteLine("IMPERSONATE with " + user);
p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Console.WriteLine(p1);
p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
Console.WriteLine(p1);
p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
Console.WriteLine(p1);
p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
Console.WriteLine(p1);
}
}
输出:
CURRENT USER MyUser
C:\Users\MyUser\Documents
C:\Users\MyUser
C:\Users\MyUser\Desktop
C:\Users\MyUser\DesktopIMPERSONATE with user2
更新:
此外,我使用 FolderBrowserDialog
和方法 ShowDialog
,但出现错误
Unable to retrieve the root folder
当我使用模拟用户时
try
{
Console.WriteLine("FolderBrowserDialog. IMPERSONATE WITH " + user);
using (var fbd = new FolderBrowserDialog())
{
Console.WriteLine(fbd.RootFolder);
p1 = System.Environment.GetFolderPath(fbd.RootFolder);
Console.WriteLine(p1);
DialogResult result = fbd.ShowDialog();
}
}
catch (System.InvalidOperationException ex)
{
Assert.AreEqual("Unable to retrieve the root folder.", ex.Message, "No se esperaba " + ex.Message);
}
输出:
FolderBrowserDialog. IMPERSONATE with user2
Desktop
我使用 LOGON32_LOGON_NEW_CREDENTIALS
(LogonType.LOGON32_LOGON_NEW_CREDENTIALS, LogonProvider.LOGON32_PROVIDER_WINNT50
) 模仿
https://platinumdogs.me/2008/10/30/net-c-impersonation-with-network-credentials/
和我得到的路径与用户 MyUser 相同。
我的代码:
using (new Impersonator(user, domain, password
, LogonType.LOGON32_LOGON_NEW_CREDENTIALS, LogonProvider.LOGON32_PROVIDER_WINNT50
))
{
Console.WriteLine("IMPERSONATE LOGON32_LOGON_NEW_CREDENTIALS with " + user);
p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Console.WriteLine(p1);
p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
Console.WriteLine(p1);
p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
Console.WriteLine(p1);
p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
Console.WriteLine(p1);
}
输出:
IMPERSONATE LOGON32_LOGON_NEW_CREDENTIALS with user2
C:\Users\MyUser\Documents
C:\Users\MyUser
C:\Users\MyUser\Desktop
C:\Users\MyUser\Desktop