C# 程序列出用户和他们各自的目录路径

标签 c# windows

我希望在 C# 中有什么方法可以作为程序来找出系统中可用的用户列表,即在“我的电脑”中以及他们的目录路径。我的意思是假设有 2 个用户

“用户A”和“用户B”

他们的路径我的意思是用户 A 的所有文档将在 D:\Documents and Settings\User A 用户 B 也是如此。

在 C# 中有什么方法可以找出用户列表及其各自目录的路径。

最佳答案

你可以这样做

string users_reg_key=
   @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\DocFolderPaths";

public string[] ListWinUsersList()
{
 //The registry key for reading user list.

 RegistryKey key =
 Registry.LocalMachine.OpenSubKey(users_reg_key, true);

 string[] winusers = "  ".Split(' ');//this resolve problem with assigned variable

 if (key != null && key.ValueCount > 0)
 {
     winusers = key.GetValueNames();
 }
 return winusers;
}

编辑

获取目录尝试这样的事情

string path =  Directory.GetParent(Environment.GetFolderPath(Environment.
SpecialFolder.ApplicationData)).FullName;
if ( Environment.OSVersion.Version.Major >= 6 ) {
 path = Directory.GetParent(path);
}

关于C# 程序列出用户和他们各自的目录路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10346126/

相关文章:

c++ - Windows 应用程序音量混合器

c# - 摆脱滚动列表框中的额外空间

c# - 如果使用 SqlServer CE 4.0 执行代码优先,数据库的位置是什么?

windows - 用于建立连接的端口数的性能计数器

c++ - 链接时 undefined reference - 缺少库?

c++ - 如何使 Microsoft VCC 在出现第一个构建错误时崩溃?

python - 创建自包含的 Python 应用程序

c# - 如何从 C# 应用程序调用带有指向 void 的指针的 C++ 函数?

c# - 基于构造函数的对象初始化

c# - 如何通过查询获得某人工作过的所有工作项?