开发者

Set SelectedPath as a variable in FolderBrowserDialog

I am trying to set the selected folder i开发者_C百科n a FolderBrowserDialog control as a variable, so I can use it within another method

The code I have so far is:

  private void button18_Click(object sender, EventArgs e)
        {
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                //
                // The user selected a folder and pressed the OK button.
                // We print the number of files found.
                //
                string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
                MessageBox.Show("Files found: " + files.Length.ToString(), "Message");
            }
        }

So I could call the selected folder in the control above in a method like this:

Process.Start("test.exe", <Folder Selection Here> );


I started looking at this before I noticed that you had requested the question to be closed. Anyway here's the code should it be useful for someone else.

    private void button1_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();

        //Choose the default start up folder
        string selectedFolder = @"C:\Dev";

        //Set that into the dialog
        folderBrowserDialog1.SelectedPath = selectedFolder;

        if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
        {
            //Grab the folder that was chosen
            selectedFolder = folderBrowserDialog1.SelectedPath;

            // The user selected a folder and pressed the OK button.
            // We print the number of files found.                
            string[] files = Directory.GetFiles(selectedFolder);

            MessageBox.Show("Files found: " + files.Length.ToString(), "Message");
            MessageBox.Show(selectedFolder);
        }            
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜