简体   繁体   English

如何使用C#切换目录?

[英]How to switch directories using C#?

(This is a continuation of the discussion on this question ) I have code that looks in a specific folder in the C: drive. (这是对这个问题的讨论的继续)我有一些代码在C:驱动器的特定文件夹中查找。 It can tell what is in that folder, and gets what the user selects. 它可以知道该文件夹中的内容,并获取用户选择的内容。 But the problem is switching to a new Data folder to get the code from. 但是问题是切换到新的Data文件夹以获取代码。

All the code necessary to run the program is kept in the Data folder, the company I am interning with wants to be able to switch Data folders so they can show their software off better, and have it geared towards whoever they are showing it to. 运行该程序所需的所有代码都保存在Data文件夹中,与我合作的公司希望能够切换Data文件夹,以便它们可以更好地展示其软件,并使其适合展示给谁。

So basically my program needs to switch data folders so the company can show their software better. 因此,基本上我的程序需要切换数据文件夹,以便公司可以更好地展示其软件。

Ill post all my code, its not much, so you guys can look at all of it. 病态发布了我所有的代码,数量不多,所以你们可以看看所有代码。

    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);
            }

        }
     }

To answer your second question, about stripping the Default Path from the combobox display 要回答您的第二个问题,关于从组合框显示中删除默认路径

//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);

Then where you set your path change to this. 然后在您设置路径的位置更改为此。 Because we removed the default Path we now have to add it again. 因为我们删除了默认路径,所以我们现在必须再次添加它。

string path =  defaultPath+comboBox1.SelectedItem.ToString();
defaultPath = path;

EDIT(@K'Leg Suggestion to make answer more clear): If you want to get the subdirectories, after you make your first selection you should call method dropdown() in comboBox1_SelectedIndexChanged 编辑(@ K'Leg建议,使答案更清楚): 如果要获取子目录,则在进行第一次选择后 ,应在comboBox1_SelectedIndexChanged调用方法dropdown()

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

A better would be receive default path as parameter in dropdown(), something on the following line 更好的方法是在dropdown()中接收默认路径作为参数,在下一行

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

and then call dropdown method in comboBox1_SelectedIndexChanged as: 然后在comboBox1_SelectedIndexChanged中调用下拉方法为:

dropdown(comboBox1.SelectedItem.ToString());

EDIT: (based on the comments on OP) the problem is the filter you are specifying for the GetDirecotries, for every path you pass to it, it looks for folder starting with Data and then any characters, for example Data.Apple, now when you set your path to Data.Apple, there it again looks for folder which should start with Data, you may pass the filter in the dropdown method on some condition 编辑:(基于OP上的注释)问题是您为GetDirecotries指定的筛选器,对于传递给它的每个路径,它都查找以Data开头的文件夹,然后是任何字符,例如Data.Apple,现在您将路径设置为Data.Apple,然后再次查找应以Data开头的文件夹,在某些情况下,您可以通过下拉方法传递过滤器

you may define the method dropdown as: 您可以将方法下拉列表定义为:

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

Then you can call the dropdown for the first time as : public Form1() { InitializeComponent(); 然后,您可以使用以下方式首次调用该下拉列表:public Form1(){InitializeComponent(); dropdown(@"C:\\Mavro\\MavBridge\\","Data*"); 下拉(@ “C:\\ Mavro \\ MavBridge \\”, “数据*”); } and then in the SelectedIndexChanged as: },然后在SelectedIndexChanged中为:

dropdown(comboBox1.SelectedItem.ToString(),"*"); // * means select all

Look at your button1_Click method. 查看您的button1_Click方法。 Change your message Box to 将您的消息框更改为

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();
        }

you will see that you have already changed the default Path 您会看到您已经更改了默认路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM