I added these three components from tools menu/choose toolbox items; however they are not working when I changed the drive or directory..

here is the code of these three components in case they change

    private void driveListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            dirListBox1.Path = driveListBox1.Drive;
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error : " + ex.Message,
                            "Error",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
        }
    }

    private void dirListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            this.fileListBox1.Path = this.dirListBox1.Path;
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error : " + ex.Message,
                            "Error",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
        }
    }

    private void fileListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string fullPath = fileListBox1.Path + "\\" + fileListBox1.FileName;
        try
        {
            StreamReader sr = new StreamReader(fullPath);
            textBox2.Text = sr.ReadToEnd();
            textBox1.Text = fullPath;
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error : " + ex.Message,
            "Error",
            MessageBoxButtons.OK,
            MessageBoxIcon.Error);
        }
    }

it is said that these components are compatible wth VB.NET, does it mean I can't use them in a C# project?

Dani AI

Generated

DriveListBox / DirListBox / FileListBox are the old VB6 file-browsing controls shipped in the Microsoft.VisualBasic.Compatibility.VB6 assembly for upgrade scenarios. They are marked obsolete and Microsoft documents that the Compatibility namespace exists only to help port VB6 code — and that those types are obsolete and supported only in 32‑bit processes. DirListBox class Obsolete types in .NET Framework. (learn.microsoft.com)

That background explains the symptoms in this thread: the Path/selected‑item behavior can be flaky (does not always update, has container/focus quirks and oddities on some shares). correctly reported inconsistent Path updates, and ’s small workaround (.Refresh()) is a known band‑aid rather than a robust fix — it may help in some hosts but will not remove underlying incompatibilities. Community threads from the VB/.NET era show similar issues when those controls are embedded or used on non‑local paths. (pcreview.co.uk)

Recommended approach:

  • Prefer the Windows dialogs or a small custom UI. Use OpenFileDialog or FolderBrowserDialog for simple file/folder selection; those are supported, current, and stable. OpenFileDialog docs. FolderBrowserDialog docs. (learn.microsoft.com)

  • For an explorer‑style view build a TreeView + ListView and enumerate with System.IO (DirectoryInfo / FileInfo). Example pattern (run IO on a background thread, update UI on the UI thread):

async Task PopulateListAsync(string path)
{
    var items = await Task.Run(() =>
    {
        var di = new System.IO.DirectoryInfo(path);
        return di.GetDirectories().Select(d => d.Name)
                 .Concat(di.GetFiles().Select(f => f.Name))
                 .ToList();
    });

    listView1.BeginUpdate();
    listView1.Items.Clear();
    foreach (var name in items) listView1.Items.Add(new ListViewItem(name));
    listView1.EndUpdate();
}

If continuing to use the compatibility controls (not recommended), add a reference to Microsoft.VisualBasic.Compatibility.dll, build the app for x86 (the docs note 32‑bit support), call Refresh after programmatic Path changes when necessary, and test heavily on network shares and on 64‑bit machines. The clean, future‑proof choice is to replace them with OpenFileDialog / FolderBrowserDialog or a small custom browser. (learn.microsoft.com)

Recommended Answers

All 3 Replies

I tried them out in 2008, and the directory browser's path does not react correctly. You can add some text boxes to your form that are populated with the new path from this component as you select or even double click around on the dir component... It fails 90% of the time to update it own Path property.
I would look for an alternative solution.

// Jerry

I have used openfiledialog class instead of the components above and it works fine now.. anyway thanx for the reply jerry..

hi - just do .Refresh() after setting the Path. seems to work fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.