我有一个WinForm应用程序,它采用两个TreeView的 struct ,并将它们作为文件夹实现到用户在下拉菜单中 Select 的路径中.
下拉菜单当前获取Z:中的所有可选文件夹
现在,我的TreeView loremPath
具有正确的驱动器Z:,但ipsumPath
应该放入R:
但使用相同的Drop Down-因为第二个Drive的文件夹 struct 是exact作为Z:所以我不需要构建一个全新的Drop Down,我只需要将ipsumPath
中的路径更改为R:,并且可以为两个Treeview使用一个Drop Down.
所以我之前在StackOverflow上有一个问题,有人建议我对两个Treeview都使用硬编码路径,但我不知道如何实现.
I tried something like:
var testPath= new DirectoryInfo("R:\\").GetDirectories();
var treeSeperator = ipsumPath.PathSeparator;
var dirSep = Path.DirectorySeparatorChar.ToString();
foreach (var node in GetCheckedNodes(ipsumPath.Nodes))
{
var sPath = Path.Combine(testPath.ToString(), node.FullPath.Replace(treeSeperator, dirSep));
Directory.CreateDirectory(sPath);
}
但这并没有起到任何作用.
My whole Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using IWshRuntimeLibrary;
using System.Reflection;
namespace Form1
{
public partial class Form1 : Form
{
[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
private static extern IntPtr CreateRoundRectRgn
(
int nLeftRect,
int nTopRect,
int nRightRect,
int nBottomRect,
int nWidthEllipse,
int nHeightEllipse
);
public Form1()
{
InitializeComponent();
this.FormBorderStyle = FormBorderStyle.None;
Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
foreach (TreeNode tn in loremPath.Nodes)
{
tn.Expand();
}
foreach (TreeNode tn in ipsumPath.Nodes)
{
tn.Expand();
}
ipsumDropDown.Items.AddRange(new[] { "R:\\", "Z:\\" });
loremDropDown.DataSource = new DirectoryInfo($"{ipsumDropDown.SelectedItem}").GetDirectories();
}
private void CreateShortcutToCurrentAssembly(string saveDir)
{
var testPath = loremDropDown.SelectedValue.ToString();
WshShell wshShell = new WshShell();
string fileName = testPath + "\\" + Application.ProductName + ".lnk";
IWshShortcut shortcut = (IWshShortcut)wshShell.CreateShortcut(fileName);
shortcut.TargetPath = Application.ExecutablePath;
shortcut.Save();
}
private void close_Click(object sender, EventArgs e)
{
System.Windows.Forms.Application.Exit();
}
private void loremPath_AfterCheck(object sender, TreeViewEventArgs e)
{
if (e.Action == TreeViewAction.Unknown) return;
foreach (TreeNode n in e.Node.Children())
n.Checked = e.Node.Checked;
foreach (TreeNode p in e.Node.Parents())
p.Checked = p.Nodes.OfType<TreeNode>().Any(n => n.Checked);
}
private IEnumerable<TreeNode> GetCheckedNodes(TreeNodeCollection nodeCol)
{
foreach (TreeNode node in nodeCol)
{
if (node.Checked ||
node.Nodes.Cast<TreeNode>().Any(n => n.Checked))
{
yield return node;
}
foreach (TreeNode childNode in GetCheckedNodes(node.Nodes))
{
if (childNode.Checked)
yield return childNode;
}
}
}
private void projektordnerGenerieren_Click(object sender, EventArgs e)
{
var destPath = loremDropDown.SelectedValue.ToString();
var treeSep = loremPath.PathSeparator;
var dirSep = Path.DirectorySeparatorChar.ToString();
foreach (var node in GetCheckedNodes(loremPath.Nodes))
{
var sPath = Path.Combine(destPath, node.FullPath.Replace(treeSep, dirSep));
Directory.CreateDirectory(sPath);
}
foreach (var node in GetCheckedNodes(ipsumPath.Nodes))
{
var sPath = Path.Combine(destPath, node.FullPath.Replace(treeSep, dirSep));
Directory.CreateDirectory(sPath);
}
string folder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
CreateShortcutToCurrentAssembly(folder);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
this.loremPath.SelectedNode = this.loremPath.Nodes[0];
this.ipsumPath.SelectedNode = this.ipsumPath.Nodes[0];
loremPath.SelectedNode.Text = textBox1.Text;
ipsumPath.SelectedNode.Text = textBox1.Text;
}
private void ipsumPath_AfterCheck(object sender, TreeViewEventArgs e)
{
if (e.Action == TreeViewAction.Unknown) return;
foreach (TreeNode n in e.Node.Children())
n.Checked = e.Node.Checked;
foreach (TreeNode p in e.Node.Parents())
p.Checked = p.Nodes.OfType<TreeNode>().Any(n => n.Checked);
}
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool ReleaseCapture();
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
private void alleErweitern_Click(object sender, EventArgs e)
{
foreach (TreeNode tn in loremPath.Nodes)
{
tn.Expand();
}
foreach (TreeNode tn in ipsumPath.Nodes)
{
tn.Expand();
}
}
private void alleReduzieren_Click(object sender, EventArgs e)
{
foreach (TreeNode tn in loremPath.Nodes)
{
tn.Collapse();
}
foreach (TreeNode tn in ipsumPath.Nodes)
{
tn.Collapse();
}
}
private void minimize_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
}
static class TreeViewExtensions
{
public static IEnumerable<TreeNode> Children(this TreeNode node)
{
foreach (TreeNode n in node.Nodes)
{
yield return n;
foreach (TreeNode child in Children(n))
yield return child;
}
}
public static IEnumerable<TreeNode> Parents(this TreeNode node)
{
var p = node.Parent;
while (p != null)
{
yield return p;
p = p.Parent;
}
}
}
}