Akon
Akon

Reputation: 272

Browsing all drive in WPF tree view

I am designing a folder explorer in WPF.

I am successful to browse a perticular drive [e:] using the below code :

<Window.Resources>
    <ObjectDataProvider x:Key="RootFolderDataProvider">
        <ObjectDataProvider.ObjectInstance>
            <folderExplorer:FolderExplorer FullPath="e:\" />
        </ObjectDataProvider.ObjectInstance>
    </ObjectDataProvider>

    <HierarchicalDataTemplate
        DataType    = "{x:Type folderExplorer:FolderExplorer}"
        ItemsSource = "{Binding Path=SubFolders}">
        <TextBlock Text="{Binding Path=Name}" />
    </HierarchicalDataTemplate>
</Window.Resources>


<TreeView Grid.Column="0"
          Name="RootTreeView"
          Background="AliceBlue"
          Foreground="Black" Grid.RowSpan="3" Margin="0,0,0,169">
    <TreeViewItem Header="Browse">
        <TreeViewItem.ItemsSource>
            <Binding Source="{StaticResource RootFolderDataProvider}">
                <Binding.Path>SubFolders</Binding.Path>
            </Binding>
        </TreeViewItem.ItemsSource>
    </TreeViewItem>
</TreeView>

How can I browse all drives, that is by Mycomputer. Let me know if any information is needed.

Upvotes: 0

Views: 6770

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222582

public void LoadDirectories()
{
    var drives = DriveInfo.GetDrives();
    foreach (var drive in drives)
    {
        this.treeView.Items.Add(this.GetItem(drive));
    }

here is the link,

WPF treeview Directory

here is the sample to bind in XAML

File explorer WPF

Upvotes: 1

Related Questions