简体   繁体   中英

File Explorer for WPF - get file directory/path

I've asked a question about a simple File Explorer for WPF yesterday: File explorer - UnauthorizedAccessException

The file explorer works fine, but now I need the filename and filepath from the selected file.

This is my xaml:

<TreeView Grid.Column="0" Width="Auto" Height="Auto" HorizontalAlignment="Stretch" Name="feDirectory" SelectedItemChanged="feDirectorySelectedItemChanged" />
<ListBox Grid.Column="1" Width="Auto" Height="Auto" HorizontalAlignment="Stretch" Name="fExpl" VerticalAlignment="Stretch" MouseDoubleClick="fExplMouseDoubleClick"/>

I can get the filename:

private void fExplMouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    string fileName = fExpl.SelectedItem.ToString();
}

But if I try to get the filepath - it doesn't work. I think the problem is, that the filepath is not shown in the ListBox, but in the TreeView. Do anybody have an idea? Should I post the whole C# code or is linking the question from yesterday ok?

Edit: Why do I need that? Because I want to open the file from the File Explorer by ProcessStartInfo and Process

There isn't a short easy answer here. You've picked up some example code with a single purpose and you are now trying to take it beyond that purpose, and so it's time for a redesign.

If you read much about WPF apps you will come across the pattern called MVVM, or Model-View-ViewModel. The idea of this is that you have some raw data - the Model, an adpater that helps you deal with it in the UI - the ViewModel, and then a UI element to display it - the View. This pattern is used because although it seems complex at first, it makes your applications much simpler as they get more complicated.

The code you have is using a more old winforms pattern of inherited view. This extends the UI element ( ListViewItem ) by inheriting from it ( MyListViewItem ), but unfortunately your code example doesn't include the definition for it.

So you could redesign your app to use MVVM and in doing so will solve lots of problems that will come up in the future, or you could simply extend the MyListViewItem to include an extra property Path but this will only get you past this problem and onto the next one.

Having a look to the code posted in other question, and I see that you fill your ListBox with :

foreach (FileInfo FL in DIR.GetFiles())
{
    ltbExplorer.Items.Add(FL.Name);
}

You can add other object than String in the ListBox, you can add directly a FileInfo like this :

foreach (FileInfo FL in DIR.GetFiles())
{
    this.fExpl.Items.Add(FL);
}

In WPF, an automatic call to ToString() method is done when an object msut be shown. So, your listbox will be filled well

Then, to get your directory, you can use directly the FileInfo object :

private void fExpl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    String dir = (fExpl.SelectedItem as FileInfo).DirectoryName;
    // assuming you want to put the directory in a texbox
    this.directory.Text = dir;
}

that's all ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM