我正在尝试创建一个简单的基本文件资源管理器/驱动器资源管理器,但遇到了一个小问题。我对 C# 编程还是很陌生,想知道是否有办法从 ListBox 中保存路径。
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
System.IO.DriveInfo dir = new System.IO.DriveInfo(comboBox1.Text);
string[] files = Directory.GetDirectories(dir.Name);
setDriveInfo();
dirView.Items.Clear();
this.currentDrive = dir.Name;
this.tempPath = this.currentDrive;
foreach (string file in files)
{
string filename = Path.GetFileName(file);
dirView.Items.Add(filename);
}
首先,我有一个组合框,列出了所有可用的驱动器。当索引更改时,ListBox 会被清除,然后它会为找到的每个目录添加一个 Item。
从那里我在 ListBox 上有一个鼠标双击事件,以检测用户何时从列表中选择“目标”目录。
private void dirView_MouseDoubleClick(object sender, MouseEventArgs e)
{
// The selected Dir the user has chosen from the drive
string dest = dirView.SelectedItem.ToString();
string path = path + comboBox1.Text + dest
string[] files = Directory.GetDirectories(path);
dirView.Items.Clear();
foreach (string file in files)
{
string filename = Path.GetFileName(file);
dirView.Items.Add(filename);
}
}
我试图让新目录显示在同一个列表框中,但我遇到的问题是路径没有正确更新或清除。第一次运行路径没有任何东西。并且路径是正确的(例如:C:/temp)但是假设我双击用户,新路径是 C:/Users 然后出现一个新列表,我双击桌面,新路径是 C:/Desktop不存在,或 C:/C:/Users/Desktop 也不存在或其他错误。
很可能它与清除路径很简单,但将临时存储在某处或某处。希望更多的眼睛可以让我更深入地了解该做什么。