如何浏览文件夹

时间:2009-09-14 08:51:24

标签: c# winforms

我想设计一个包含浏览按钮的程序,我们可以浏览所选文件夹并打开文件夹中的文件。

我需要一个参考和阅读,我可以解决我的问题?喜欢我应该使用什么方法/类。我不喜欢从MSDN上读书,因为我很难理解他们的理论。仅供参考我仍然是C#的初学者。

非常感谢

P / s:这是我从互联网上找到的代码,您可以浏览/创建新文件夹。但我不知道为什么它使用Shell32.dll ..

private void button1_Click(object sender, EventArgs e)
{
    string strPath;
    string strCaption = "Select a Directory and folder.";
    DialogResult dlgResult;
    Shell32.ShellClass shl = new Shell32.ShellClass();
    Shell32.Folder2 fld = (Shell32.Folder2)shl.BrowseForFolder(0, strCaption, 0,
        System.Reflection.Missing.Value);
    if (fld == null)
    {
        dlgResult = DialogResult.Cancel;
    }
    else
    {
        strPath = fld.Self.Path;
        dlgResult = DialogResult.OK;
    }
}

4 个答案:

答案 0 :(得分:10)

来自msdn

private void button1_Click(object sender, System.EventArgs e)
{
    Stream myStream = null;
    OpenFileDialog openFileDialog1 = new OpenFileDialog();

    openFileDialog1.InitialDirectory = "c:\\" ;
    openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
    openFileDialog1.FilterIndex = 2 ;
    openFileDialog1.RestoreDirectory = true ;

    if(openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        try
        {
            if ((myStream = openFileDialog1.OpenFile()) != null)
            {
                using (myStream)
                {
                    // Insert code to read the stream here.
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
        }
    }
}

答案 1 :(得分:4)

            string folderpath = "";
            FolderBrowserDialog fbd = new FolderBrowserDialog();

            fbd.ShowNewFolderButton = false;
            fbd.RootFolder = System.Environment.SpecialFolder.MyComputer;
            DialogResult dr = fbd.ShowDialog();

            if (dr == DialogResult.OK)
            {
                folderpath = fbd.SelectedPath;
            }

            if (folderpath != "")
            {
                txtBoxPath.Text = folderpath; 
            }

答案 2 :(得分:2)

从工具箱中将FolderBrowserDialog组件拖到表单中,并将其命名为folderBrowserDialog。在您的浏览按钮事件处理程序中编写以下代码。

    private void btnBrowseBackupLocation_Click(object sender, EventArgs e)
    {
        DialogResult result = folderBrowserDialog.ShowDialog();
        if (result == DialogResult.OK)
        {
            txtboxBackupLocation.Text = folderBrowserDialog.SelectedPath;
        }
    }

答案 3 :(得分:0)

要在名为“Browse_Button”的按钮上单击名为“ARfilePath”的文本框中的文件名插入文件路径,上述代码将被修改为:

    private void Browse_Button_Click(object sender, EventArgs e)
    {
        Stream myStream = null;
        OpenFileDialog openFileDialog1 = new OpenFileDialog();

        openFileDialog1.InitialDirectory = "c:\\";
        openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
        openFileDialog1.FilterIndex = 2;
        //openFileDialog1.RestoreDirectory = true;
        Boolean FileExist=openFileDialog1.CheckFileExists;
        Boolean PathExist=openFileDialog1.CheckPathExists;
        openFileDialog1.FileName = null;
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            try
            {
                if ((myStream = openFileDialog1.OpenFile()) != null)
                {
                    using (myStream)
                    {
                        if (FileExist == true && PathExist == true)
                        {
                            // Insert code to read the stream here.
                            string Pathname = openFileDialog1.FileName;
                            ARfilePath.Text = Pathname;
                            ARfilePath.Enabled = false;
                            /*DISABLED SO THAT THE USER CANNOT MAKE UNNECESSARY CHANGES IN THE FIELD*/
                        }
                    }
                }
            }
            catch (Exception ex)
            {

                /*SHOW ERRORS TO USER*/ error_label.Text = "Error: Could not read file from disk. Original error: " + ex.Message;

                //MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
            }
        }
    }