简体   繁体   中英

FilePicker return name and path as strings

I've got a file Save and file open picker that im now trying to integrate the ability to save the Path and FileName as public variable that will be used across the whole project through different methods etc.

I've currently got a SaveFileClass and OpenFileClass .

I've seen examples of using the OpenFileDialog to return the save directory although I don't believe these are suitable for what im after. Maybe in some shape or form but dont seem to make much sense for the FileOpenPicker and FileSavePicker I have in use currently.

What I have currently (minus the returning directories) is this:

public async Task<IStorageFile> OpenFileAsync()
{
    FileOpenPicker openPicker = new FileOpenPicker
    {
        ViewMode = PickerViewMode.List,
        SuggestedStartLocation = PickerLocationId.DocumentsLibrary
    };

    openPicker.FileTypeFilter.Add(".txt");
    openPicker.FileTypeFilter.Add(".csv");

    return await openPicker.PickSingleFileAsync();
}

This passes back to the MainPage.

Within here, i would like to have a variable to store the selected file path and the selected file name as a string. These will then be used around the project when it comes to quick saving/auto saving and when building my class to load files.

Im just after whether or not FilePicker has this functionality because my understanding of the documentation is a little limited when trying to integrate it with my scenario.

Your OpenFileAsync method returns a selected IStorageFile and this one has a Name property that gets you the name of the file including the file name extension and a Path property that gets you the full file-system path of the file. You can do whatever you want with these values:

private async void OpenFile_Click(object sender, RoutedEventArgs e)
{
    OpenFileClass instance = new OpenFileClass();
    IStorageFile file = await instance.OpenFileAsync();
    if (file != null)
    {
        string fileName = file.Name;
        string filePath = file.Path;
    }
}

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