如何使用C#切换目录? [英] How to switch directories using C#?

查看:668
本文介绍了如何使用C#切换目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

(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.

运行程序所需的所有代码都保存在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.

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

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;

这篇关于如何使用C#切换目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆