0

(これは、この質問に関する議論の続きです)C:ドライブの特定のフォルダーを検索するコードがあります。そのフォルダに何があるかを認識し、ユーザーが選択したものを取得します。しかし、問題は、コードを取得するために新しいデータフォルダーに切り替えることです。

プログラムの実行に必要なすべてのコードはデータフォルダーに保存されています。私がインターンしている会社は、データフォルダーを切り替えて、ソフトウェアをよりよく見せることができ、誰に見せてもよいように調整できるようにしたいと考えています。

したがって、基本的に私のプログラムは、会社がソフトウェアをより適切に表示できるように、データフォルダを切り替える必要があります。

私のすべてのコードを投稿しますが、それほど多くはないので、皆さんはすべてを見ることができます。

    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

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

編集(@ K'Leg提案で回答をより明確にする):サブディレクトリを取得する場合は、最初の選択を行った後、でdropdown()メソッドを呼び出す必要がありますcomboBox1_SelectedIndexChanged

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

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(); dropdown(@ "C:\ Mavro \ MavBridge \"、 "Data *"); 次に、SelectedIndexChangedで次のようになります。

dropdown(comboBox1.SelectedItem.ToString(),"*"); // * means select all
于 2012-05-09T16:02:35.883 に答える
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 に答える