Monday, February 25, 2008

A custom OpenFileDialog implementation that browses the full file system: MobilePractices.OpenFileDialogEx

Doing a spike yesterday I had the need to use OpenFileDialog to browse and select an existing file. The problem was that I placed the files on the device root. And oops! the Windows Mobile implementation of the OpenFileDialog, this is not a .Net Compact Framework limitation, doesn't allow the user to browse the root. Actually, it doesn't allow the user to browse the whole file system, it just let you browse My Documents subfolders and Storage cards. Very bothering in some cases.

Indeed, this is not the first time I have to deal with it. An I thought I should be just one of many developers with the same problem. So I decided to implement a custom OpenFileDialog in c# targeting PocketPC, but easily portable to Smartphone, and customizable and extensible as well. Wow! I think I should have said that first of all, it's a very simple piece of code. And that's its best feature. You can use it as a starting point.

I've not implemented the full Filter feature of the original OpenFileDialog because I think it's not a common scenario on mobile development and it make the UI overcrowded. Instead, the Filter property of my MobilePractices.OpenFileDialogEx implementation, is just a search pattern.

The MobilePractices.OpenFileDialogEx looks this way:

image

The dialog title can be changed as you wish thru the Text property. At the top of the screen, you can see a ComboBox which can be used to quickly go back to an specific folder.

image

Taking a look at the first snapshot you can notice that I'm identifying storage cards with a different icon. To identify a storage card you can check if the FileInfo.Attributes for it are Directory and Temporary.

if ((info.Attributes & (FileAttributes.Directory | FileAttributes.Temporary))
== (FileAttributes.Directory | FileAttributes.Temporary))
return FileSystemKind.StorageCard;

How to continue with this is up to you. You can add an InitialDirectory property, or some way to keep the current path along the application run, or a modified version which allows you to select a directory instead of a file. Or you can take the code and port it to smartphone. Probably I'll do that soon in a new post. Once more, this is just a starting point!


Here you have the source code including a sample application:



Enjoy!

5 comments:

Shahal H said...

I would only like to point out that when searching for folders with the Temporary attribute, you may encounter the Bluetooth folder in Windows Mobile 6 (I'm not sure if it is supported in Windows Mobile 5). If enabled on the device, it appears to have the same attributes as a storage card.

Anonymous said...

Muchisimas gracias por tu blog. Estoy realizando mi primera aplicación para un dispositivo movil y me estas salvando la vida.
Saludos desde Málaga

Anonymous said...

Is possible using this for save the file that i can create, too. With a few modify you can save your file in anywhere directory

Anonymous said...

Could you wrote in VBnet code?

Дмитрий Франк said...

thank you very much for your job.