问题
(This is a continuation of the discussion on this question) I have code that looks in a specific folder in the C: drive. 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.
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.
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);
}
}
}
回答1:
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;
回答2:
Look at your button1_Click method. 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
回答3:
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
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
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:
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
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(); dropdown(@"C:\Mavro\MavBridge\","Data*"); } and then in the SelectedIndexChanged as:
dropdown(comboBox1.SelectedItem.ToString(),"*"); // * means select all
来源:https://stackoverflow.com/questions/10519704/how-to-switch-directories-using-c