maui icon indicating copy to clipboard operation
maui copied to clipboard

FilePicker default directory

Open DevilDog2098 opened this issue 3 years ago • 7 comments

Description

I would love to have the ability to set a default directory in the FilePicker type. i.e. Set FilePicker to a specific directory when it displays to the user.

Public API Changes

public class PickOptions { public PickOptions();

    public static PickOptions Default { get; }


    public static PickOptions Images { get; }

    public string? PickerTitle { get; set; }     

    public FilePickerFileType? FileTypes { get; set; }

   --> public string BaseDirectory? {get; set;}
}

}

Intended Use-Case

In my application I have the ability for users to Open and Backup/Restore project files. Currently when "Open Project" is selected the FilePicker opens in the Documents directory. Being able to set a default directory for the picker to open too would save a lot of support issues once a production release is pushed out.

DevilDog2098 avatar Aug 05 '22 07:08 DevilDog2098

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

ghost avatar Feb 20 '23 15:02 ghost

I would like to see this feature as well. For my case, I have found that if the last-chosen path was one on the network, it can take a while to load the file picker if the user is not connected to the VPN. I would leverage a cancellation token to check that the file picker UI is shown within a reasonable time to set the starting path so it is the user's home (e.g., %USERPROFILE% on Windows or $HOME on Linux and macOS) if there is a timeout.

mavaddat avatar Nov 02 '23 02:11 mavaddat

I also would like to have this feature, so that the user doesn't need to take care about the directory anymore. Is there any workaround?

taimo42 avatar Nov 24 '23 11:11 taimo42

I have done some research, and it appears the problem with this feature request is that the platform-specific implementations of the file/folder picker are inconsistent about providing an option to specify the starting directory.

Operating System C# SDK Namespace for file picker File/Folder picker allows specifying start directory Qualififications/Caveats API Documentation
Windows Windows.Storage.Pickers No The SuggestedStartLocation property only allows you to set predefined locations. Windows.Storage.Pickers
Android Android.Content Sometimes The ACTION_GET_CONTENT does not support opening in a specific location; however,Intent#ACTION_OPEN_DOCUMENT Intent#ACTION_CREATE_DOCUMENT Intent#ACTION_OPEN_DOCUMENT_TREE do support it through the EXTRA_INITIAL_URI intent extra. Android.Content
iOS UIKit.UIDocumentPickerViewController Yes You can specify the starting directory by setting the directoryURL property. However, this property has no effect in Mac apps built with Mac Catalyst. UIDocumentPickerViewController
Tizen Tizen.Applications.FilePicker No The API does not provide a way to specify a custom start path for the file picker. Tizen.Applications.FilePicker
MacCatalyst UIKit.UIDocumentPickerViewController No The directoryURL property has no effect in Mac apps built with Mac Catalyst. UIDocumentPickerViewController
macOS AppKit.NSOpenPanel No The NSOpenPanel does not support opening in a specific location programmatically. NSOpenPanel
tvOS UIKit.UIDocumentPickerViewController Unknown I couldn't find specific information on how to specify a starting directory for the file picker in tvOS. UIDocumentPickerViewController
watchOS WatchKit.WKInterfacePicker No In watchOS, you can use the Photos picker view to browse and select images and videos from the photo library. However, it doesn't seem like you can specify a starting directory. WKInterfacePicker

Since the standard APIs provided by the OS C# SDK are inconsistent on this point, implementing this feature in a cross-platform UI framework like .NET MAUI would require developing custom file-picker libraries. I think going down this road would make MAUI opinionated about UI implementations and be opposed to the generic, flexible design goals of .NET MAUI.

mavaddat avatar Nov 24 '23 22:11 mavaddat

@mavaddat I haven't checked yet all of the linked documentations, but isn't this somewhat covered in Android by the "EXTRA_INITIAL_URI" Parameter when calling the "ACTION_OPEN_DOCUMENT" Intent?

DDHSchmidt avatar May 17 '24 16:05 DDHSchmidt

Isn't this somewhat covered in Android by the "EXTRA_INITIAL_URI" Parameter when calling the "ACTION_OPEN_DOCUMENT" Intent?

Yes, it looks like you are correct. Although the ACTION_GET_CONTENT does not support opening in a specific location, it looks like Intent#ACTION_OPEN_DOCUMENT Intent#ACTION_CREATE_DOCUMENT Intent#ACTION_OPEN_DOCUMENT_TREE supports it through the EXTRA_INITIAL_URI intent extra. I have updated the table above.

mavaddat avatar May 17 '24 17:05 mavaddat

I also need this feature. I just wrote my very first MAUI app (a short 300 lines of code) for a friend to assist him with a tedious, mundane task on his Mac. The very first thing he asked for when I sent him the application, was to be able to have it come up with a default folder when opening a file.

EricHarmon avatar Jun 29 '24 13:06 EricHarmon

To add more details to the excelent writeup https://github.com/dotnet/maui/issues/9212#issuecomment-1826125726:

  • Regarding the Mac Catalyst platform, there is https://developer.apple.com/documentation/uikit/uidocumentpickerviewcontroller/directoryurl#Discussion and unfortunately directoryURL has no effect on Mac Catalyst. Not sure why they claim that directoryURL is supported on Mac Catalyst 13.1 then.
  • Regarding the Windows platform, it seems one can actually work it around using this approach: https://github.com/microsoft/WindowsAppSDK/issues/88#issuecomment-2604411467

MartyIX avatar Jan 21 '25 18:01 MartyIX

To add more details to the excelent writeup #9212 (comment):

The problem is that the workaround (IFileDialog::SetFolder or IFileDialog::SetDefaultFolder) is not part of the standard Windows.Storage.Pickers API used in most .NET MAUI applications and requires using lower-level Windows APIs.

The IFileDialog::SetFolder API is a COM-based API native to C++ and is not directly available within .NET MAUI's C# abstractions or the Windows.Storage.Pickers namespace. Using this API in a .NET MAUI application would require significant effort and re-engineering. Here’s why:

Challenges with Using IFileDialog::SetFolder in .NET MAUI

  1. Interop Layer:

    • The IFileDialog::SetFolder API is a native Windows COM API. To use it in .NET (and by extension, MAUI), you would need to create an interop layer using P/Invoke or COM interop to bridge between C++ and C#.
    • This requires understanding and managing COM objects, memory, and lifetimes, which adds complexity.
  2. Custom Implementation:

    • MAUI is designed as a cross-platform framework. It abstracts platform-specific behavior into interfaces and renderers.
    • To use IFileDialog::SetFolder, you would need to implement a Windows-specific custom file picker interface, bypassing the existing Windows.Storage.Pickers.
  3. Breaking Abstractions:

    • Introducing a custom file picker implementation for Windows would deviate from MAUI’s philosophy of providing a unified, platform-agnostic API.
    • This could introduce inconsistencies and limit portability for developers expecting uniform behavior across platforms.
  4. Compatibility and Maintenance:

    • Integrating this functionality could complicate future updates and maintenance. Any changes to the Windows API or MAUI framework could require additional work to keep the custom implementation functional.

Feasibility in MAUI

While possible, implementing this would essentially require rewriting parts of the Windows-specific backend for file picking in MAUI. Developers would need to:

  • Use Windows-specific libraries like Windows.Win32 (via CsWin32 or WindowsAppSDK interop).
  • Provide a custom interface for Windows file pickers that exposes the functionality of IFileDialog::SetFolder.

Practical Alternative

For developers seeking this functionality without rewriting MAUI's internals, a viable approach is:

  1. Create a platform-specific implementation for Windows within a MAUI app using dependency services.
  2. Leverage the IFileDialog::SetFolder API through a helper library like Vanara.PInvoke to simplify the interop.

mavaddat avatar Jan 21 '25 18:01 mavaddat

There is also https://github.com/microsoft/WindowsAppSDK/pull/4942 (https://github.com/microsoft/WindowsAppSDK/pull/4942#discussion_r1917585940) which seems that one day there will be a picker with the feature provided out of the box.

MartyIX avatar Jan 21 '25 19:01 MartyIX

Quick update for Windows:

  • https://github.com/microsoft/WindowsAppSDK/pull/5155
  • https://github.com/microsoft/WindowsAppSDK/pull/5547 (my understanding is that 1.8-exp3 should contain this feature)

MartyIX avatar Jun 27 '25 07:06 MartyIX