0

(这是关于这个问题的讨论的延续)我有在 C: 驱动器中的特定文件夹中查找的代码。它可以判断该文件夹中的内容,并获取用户选择的内容。但问题是切换到一个新的数据文件夹来获取代码。

运行程序所需的所有代码都保存在 Data 文件夹中,我正在实习的公司希望能够切换 Data 文件夹,以便他们可以更好地展示他们的软件,并让它面向任何他们展示它的人。

所以基本上我的程序需要切换数据文件夹,这样公司才能更好地展示他们的软件。

把我所有的代码贴出来,不多,大家可以看一下。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
             string defaultPath = @"C:\Mavro\MavBridge\";


            public Form1()
            {
                InitializeComponent();
                 dropdown();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                //some sort of code to switch directory before close goes here
                MessageBox.Show("Data folder has been changed.", "Done");
                Application.Exit();
            }

            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                 string path =  comboBox1.SelectedItem.ToString();

                 defaultPath = path;
            }

            private void buttonTest_Click_1(object sender, EventArgs e)
            {


            }
            public void dropdown()
            {
                string[] dispDirectories = Directory.GetDirectories(defaultPath, "Data*");
                comboBox1.Items.Clear();
                comboBox1.Items.AddRange(dispDirectories);
            }

        }
     }
4

3 回答 3

2

要回答您的第二个问题,关于从组合框显示中删除默认路径

//Where you load your directories
string[] dispDirectories = Directory.GetDirectories(@"c:\", "*.*");
// so here we will iterate through all the directories found and remove the default path from it.
for (int i=0;i<dispDirectories.Count();i++)
dispDirectories[i]=dispDirectories[i].Remove(0, defaultPath.Length);

然后,您将路径更改为此。因为我们删除了默认路径,所以现在必须再次添加它。

string path =  defaultPath+comboBox1.SelectedItem.ToString();
defaultPath = path;
于 2012-05-09T18:26:57.673 回答
1

查看您的 button1_Click 方法。将您的消息框更改为

MessageBox.Show("Data folder has been changed to "+defaultPath,"Done");

private void button1_Click(object sender, EventArgs e)
        {
            //some sort of code to switch directory before close goes here
            MessageBox.Show("Data folder has been changed to "+defaultPath,"Done");
            Application.Exit();
        }

你会看到你已经改变了默认路径

于 2012-05-09T16:16:49.057 回答
1

编辑(@K'Leg 建议使答案更清楚):如果您想获取子目录,在您进行第一次选择后,dropdown()您应该调用方法comboBox1_SelectedIndexChanged

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                 string path =  comboBox1.SelectedItem.ToString();
                 defaultPath = path;
                 dropdown();
            }

更好的是在下拉()中接收默认路径作为参数,在下一行

public void dropdown(string defaultPath)
            {
                string[] dispDirectories = Directory.GetDirectories(defaultPath, "Data*");
                comboBox1.Items.Clear();
                comboBox1.Items.AddRange(dispDirectories);
            }

然后在 comboBox1_SelectedIndexChanged 中调用下拉方法:

dropdown(comboBox1.SelectedItem.ToString());

编辑:(基于对 OP 的评论)问题是您为 GetDirecotries 指定的过滤器,对于您传递给它的每个路径,它都会查找以 Data 开头的文件夹,然后是任何字符,例如 Data.Apple,现在当您将路径设置为 Data.Apple,它会再次查找应以 Data 开头的文件夹,您可以在某些情况下通过下拉方法中的过滤器

您可以将方法下拉列表定义为:

public void dropdown(string defaultPath, string filter)
        {
            string[] dispDirectories = Directory.GetDirectories(defaultPath, filter);
            comboBox1.Items.Clear();
            comboBox1.Items.AddRange(dispDirectories);
        }

然后你可以第一次调用下拉菜单: public Form1() { InitializeComponent(); 下拉(@"C:\Mavro\MavBridge\","数据*"); } 然后在 SelectedIndexChanged 为:

dropdown(comboBox1.SelectedItem.ToString(),"*"); // * means select all
于 2012-05-09T16:02:35.883 回答