C#从注册表中的路径递归填充treeview
问题:如何使用C#将多个路径值作为字符串从注册表递归填充到treeview节点 我想做什么: 递归地使用列表[]数组中收集的每个注册表值的字符串值填充我的treeView1,该数组也用于列表框(listBox1),当然,只添加注册表中非空的值。我非常感谢在这个问题上给予的任何帮助或意见 我如何将路径加载到TreeView中1:C#从注册表中的路径递归填充treeview,c#,recursion,path,treeview,registry,C#,Recursion,Path,Treeview,Registry,问题:如何使用C#将多个路径值作为字符串从注册表递归填充到treeview节点 我想做什么: 递归地使用列表[]数组中收集的每个注册表值的字符串值填充我的treeView1,该数组也用于列表框(listBox1),当然,只添加注册表中非空的值。我非常感谢在这个问题上给予的任何帮助或意见 我如何将路径加载到TreeView中1: private void PopulateTreeView() { try {
private void PopulateTreeView()
{
try
{
TreeNode rootNode;
NodeInfo nInfo;
string path = Global.GetStartUpPath();
DirectoryInfo info = new DirectoryInfo(path);
if (info.Exists)
{
rootNode = new TreeNode(info.Name, 3, 3);
rootNode.Name = info.Name;
nInfo = new NodeInfo(NodeInfo.Types.Root, info.FullName);
rootNode.Tag = nInfo;
GetDirectories(info, rootNode);
treeView1.Nodes.Add(rootNode);
treeView1.SelectedNode = rootNode;
}
}
我如何设置路径:
public static void setStartUpPath(string path)
{
//Open the registry key
RegistryKey rk1 = Registry.CurrentUser.OpenSubKey(@"Software", true);
//Create a subkey, if not exist
RegistryKey sk1 = rk1.CreateSubKey(@"Example\Test\Path");
sk1.SetValue("StartupPath", path);
sk1.Close();
_startUpPath = path;
}
public static string GetStartUpPath()
{
if ( (_startUpPath != null) && (_startUpPath != string.Empty) )
return _startUpPath;
//Check registry
else
{
//Open the registry key
RegistryKey rk1 = Registry.CurrentUser.OpenSubKey(@"Software\Example\Test\Path", true);
if (rk1 == null)
return string.Empty;
_startUpPath = (string)rk1.GetValue("StartupPath");
rk1.Close();
return _startUpPath;
}
}
public static string[] GetPaths()
{
//Open the registry key
RegistryKey rk1 = Registry.CurrentUser.OpenSubKey(@"Software\Example\Test\Path\List", true);
if (rk1 == null)
return null;
string[] list = new string[rk1.ValueCount];
for (int i = 0; i < rk1.ValueCount; i++)
{
list[i] = rk1.GetValue(i.ToString()).ToString();
}
rk1.Close();
return list;
}
以及我是如何得到它的:
public static void setStartUpPath(string path)
{
//Open the registry key
RegistryKey rk1 = Registry.CurrentUser.OpenSubKey(@"Software", true);
//Create a subkey, if not exist
RegistryKey sk1 = rk1.CreateSubKey(@"Example\Test\Path");
sk1.SetValue("StartupPath", path);
sk1.Close();
_startUpPath = path;
}
public static string GetStartUpPath()
{
if ( (_startUpPath != null) && (_startUpPath != string.Empty) )
return _startUpPath;
//Check registry
else
{
//Open the registry key
RegistryKey rk1 = Registry.CurrentUser.OpenSubKey(@"Software\Example\Test\Path", true);
if (rk1 == null)
return string.Empty;
_startUpPath = (string)rk1.GetValue("StartupPath");
rk1.Close();
return _startUpPath;
}
}
public static string[] GetPaths()
{
//Open the registry key
RegistryKey rk1 = Registry.CurrentUser.OpenSubKey(@"Software\Example\Test\Path\List", true);
if (rk1 == null)
return null;
string[] list = new string[rk1.ValueCount];
for (int i = 0; i < rk1.ValueCount; i++)
{
list[i] = rk1.GetValue(i.ToString()).ToString();
}
rk1.Close();
return list;
}
(如果有帮助)获取列表框路径的方法:
public static void setStartUpPath(string path)
{
//Open the registry key
RegistryKey rk1 = Registry.CurrentUser.OpenSubKey(@"Software", true);
//Create a subkey, if not exist
RegistryKey sk1 = rk1.CreateSubKey(@"Example\Test\Path");
sk1.SetValue("StartupPath", path);
sk1.Close();
_startUpPath = path;
}
public static string GetStartUpPath()
{
if ( (_startUpPath != null) && (_startUpPath != string.Empty) )
return _startUpPath;
//Check registry
else
{
//Open the registry key
RegistryKey rk1 = Registry.CurrentUser.OpenSubKey(@"Software\Example\Test\Path", true);
if (rk1 == null)
return string.Empty;
_startUpPath = (string)rk1.GetValue("StartupPath");
rk1.Close();
return _startUpPath;
}
}
public static string[] GetPaths()
{
//Open the registry key
RegistryKey rk1 = Registry.CurrentUser.OpenSubKey(@"Software\Example\Test\Path\List", true);
if (rk1 == null)
return null;
string[] list = new string[rk1.ValueCount];
for (int i = 0; i < rk1.ValueCount; i++)
{
list[i] = rk1.GetValue(i.ToString()).ToString();
}
rk1.Close();
return list;
}
public静态字符串[]getpath()
{
//打开注册表项
RegistryKey rk1=Registry.CurrentUser.OpenSubKey(@“Software\Example\Test\Path\List”,true);
如果(rk1==null)
返回null;
字符串[]列表=新字符串[rk1.ValueCount];
对于(int i=0;i
我自己解决了。对于来这里寻找答案的其他人来说,这就是我解决treeview的方法
try
{
TreeNode rootNode;
NodeInfo nInfo;
string[] paths = Global.GetPaths();
for (int i = 0; i < paths.Length; i++)
{
string path = paths[i];
DirectoryInfo info = new DirectoryInfo(path);
if (info.Exists)
{
rootNode = new TreeNode(info.Name, 3, 3);
rootNode.Name = info.Name;
nInfo = new NodeInfo(NodeInfo.Types.Root, info.FullName);
rootNode.Tag = nInfo;
GetDirectories(info, rootNode);
treeView1.Nodes.Add(rootNode);
treeView1.SelectedNode = rootNode;
}
}
}
catch (Exception ex)
{
//.....
Logic.Log.write("ERROR PopulateTreeView -" + ex.Message);
}
试试看
{
树状根结;
诺德尼福;
string[]path=Global.getpath();
for(int i=0;i
我自己解决了。对于来这里寻找答案的其他人来说,这就是我解决treeview的方法
try
{
TreeNode rootNode;
NodeInfo nInfo;
string[] paths = Global.GetPaths();
for (int i = 0; i < paths.Length; i++)
{
string path = paths[i];
DirectoryInfo info = new DirectoryInfo(path);
if (info.Exists)
{
rootNode = new TreeNode(info.Name, 3, 3);
rootNode.Name = info.Name;
nInfo = new NodeInfo(NodeInfo.Types.Root, info.FullName);
rootNode.Tag = nInfo;
GetDirectories(info, rootNode);
treeView1.Nodes.Add(rootNode);
treeView1.SelectedNode = rootNode;
}
}
}
catch (Exception ex)
{
//.....
Logic.Log.write("ERROR PopulateTreeView -" + ex.Message);
}
试试看
{
树状根结;
诺德尼福;
string[]path=Global.getpath();
for(int i=0;i