본문 바로가기
스마트 장치 개발

file dialog for C#

by leo21c 2009. 12. 16.
SMALL

참고싸이트:http://msdn.microsoft.com/en-us/library/aa446567.aspx

1260962005_SmartPhoneFilesSampleSetup.zip



Working with files on Smartphone devices with the .NET Compact Framework


Author:
Peter Foot
Microsoft Embedded MVP
In The Hand

March 2004

Applies to:
Microsoft?? .NET Compact Framework 1.0
Microsoft?? Visual Studio?? .NET 2003
Smartphone 2003

DownloadSample


Summary:This article will examine some of the considerations when building file based applications for the Smartphone platform with managed code. This will include some code examples you can use to build in file browsing to your .NET Compact Framework applications.

Contents

Introduction
The System.IO Namespace
Special Folders
File Attributes
Storage Cards
Browsing for Files
Sample Code
Conclusion
Additional Resources

Introduction

While the Smartphone platform shares many features with its bigger brother the Pocket PC, it differs somewhat in its file management functionality. By default a Smartphone does not include a File Explorer application and there are no common file dialog boxes available to the developer. If your application involves opening data files you will need some method of browsing and selecting data files. Another consideration for Smartphone developers is that these devices generally have less on-board storage memory than a basic Pocket PC. Depending on your application you can use either removable storage cards or retrieve data via the mobile network.

This article will examine some of the considerations when building file based applications for the Smartphone platform with managed code. This will include some code examples you can use to build in file system browsing to your .NET Compact Framework applications.

The System.IO Namespace

The .NET Compact Framework contains a powerful set of classes for working with the file system. These include file and directory manipulation and file reading and writing. Using a combination of these classes it is possible to navigate the folders in the file system, list files and determine their properties. The following sections will look at some uses of these classes however, in this section I will demonstrate some direct uses.

Special folders

On any Windows based systems there are a number of special folders used by the system, the exact location of which can vary between systems and locales. For example many applications will need to use the user's d0cuments folder. The Win32 API includes a function to determine the full path of a special folder. This is not directly implemented in the .NET Compact Framework but can be used via Platform Invoke.

public static string GetFolderPath(SpecialFolder folder){      //buffer to fill with path      StringBuilder path = new StringBuilder(MaxPath);      //pass stringbuilder and folder identifier to api function      if(!Convert.ToBoolean(SHGetSpecialFolderPath(IntPtr.Zero,         path, (int)folder, 0)))      {         throw new Exception("Cannot get folder path!");      }      return path.ToString();}[DllImport("coredll", EntryPoint="SHGetSpecialFolderPath",   SetLastError=false)]internal static extern int SHGetSpecialFolderPath(IntPtr hwndOwner,   StringBuilder lpszPath, int nFolder, int fCreate);

File Attributes

TheFileclass available in theSystem.IOnamespace in the .NET Compact Framework is a subset of that available on the full desktop .NET Framework. One of the key differences is the lack of methods to get and set file attributes. However this does not mean the functionality is absent from the .NET Compact Framework. As with many cases in the full .NET Framework there is more than one way to access specific functionality, to keep the .NET Compact Framework runtime small many of these duplications are excluded. Therefore to retrieve the file attributes in the .NET Compact Framework one must use theFileInfoclass instead. When retrieving files to display to the user, those marked asHiddenwill probably need to be excluded from the user interface. Therefore aFileInfoinstance can be created for the file in question to retrieve its attributes:

//get info on fileFileInfo fi = new FileInfo(filename);//don't add hidden files to the listif((fi.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden){   //code for working on file goes here}

Storage Cards

To both Pocket PC and Smartphone devices Storage Cards are very important, but there is no consistent method of addressing the storage card on the device. A further complication is that several Pocket PC devices include multiple memory card slots. Therefore if you are working with files on removable storage you need a way to determine the names of the storage cards at runtime. While this is possible by Platform Invoking a couple of API functions (FindFirstFlashCard,FindNextFlashCard) this is actually not necessary because it is possible directly with the functionality available in theSystem.IOnamespace.

Because all storage cards are addressed as subfolders of the root directory "\" and have the Directory and Temporary attributes we can easily enumerate all the top level folders, check their attributes and return an array of those which meet this criteria. The code sample below shows a function to do just that:

public static string[] GetStorageCardNames(){   ArrayList scards = new ArrayList();   DirectoryInfo rootDir = new DirectoryInfo(@"\");   foreach(DirectoryInfo di in rootDir.GetDirectories() )   {      //if directory and temporary      if ( (di.Attributes & attrStorageCard) == attrStorageCard )      {         //add to collection of storage cards         scards.Add(di.Name);      }   }   return (string[])scards.ToArray(typeof(string));}

Browsing for files

Few applications for the Smartphone are traditionally file based, instead offering a list of specific types of objects, these could include for example Contacts, Voice Recordings, Music etc. Because the on-board memory of the device is quite restrictive (especially with media files such as audio, video and photos, the user will probably use Storage Cards to provide further capacity. Because these might be shared with other devices such as digital cameras or PDAs we should not make assumptions about the location of files on such a card. It is therefore necessary in some scenarios to provide the user with a mechanism to browse the file system.

The .NET Compact Framework includes a common dialog for selecting a file to be opened, however this dialog is not available for the Smartphone platform. The full .NET Framework offers aFolderBrowserDialogas a quick method to choose a specific folder, this is not present on the .NET Compact Framework at all.

In this article a solution will be presented which uses a two staged approach to choosing a file offering the greatest flexibility and making best use of the available screen space. Rather than a single dialog we will present two complementary dialogs to be used for browsing for a folder, and selecting a file within a specific folder.

TheFolderBrowserDialogis a custom form which is based around a full screenTreeViewcontrol. This is used to populate a graphical representation of the file system hierarchy on the device. Because crawling through the entire file system is resource intensive and would introduce a long delay in the loading process for the dialog the tree is instead populated dynamically. As the user navigates the tree, its subfolders are enumerated and added to the tree. Final selection is achieved using theMenuBarcontrol which uses the two soft keys on the device. The left key is used to provide an affirmative selection; the right has a menu with other options such as the ability to create a new folder.

Figure 1 - FolderBrowserDialog

With the folder level navigation complete an interface is required for selecting from a group of specific file types in that folder. Like theFolderBrowserDialogthis has a very simple interface, this time using aListViewcontrol to present a list of files of the specified type. Types are specified using aFilterproperty in the same format as the standardOpenFileDialogon other platforms. Again the directional pad and two soft keys are the primary means of navigating the list.

Figure 2 - OpenFileDialog

The wrapper classOpenFileDialogcombines these two separate forms into a single callable dialog. This defaults to listing files in the user's d0cuments folder and allows them to change folders from a soft key menu option.

Sample Code

The sample code solution which accompanies this article contains a basic text viewing application which uses theOpenFileDialogas constructed above. The solution includes the full source code for the dialogs used which implements a number of the file techniques discussed previously.

Conclusion

While the Smartphone platform doesn't provide the same range of common dialogs available on other platforms for file operations it is relatively easy to make use of the functionality in theSystem.IOnamespace to build this functionality into your application. The sample code included with this article has shown how it is possible to integrate these file system concepts into reusable dialogs for the Smartphone platform.

Additional Resources

For more information about Windows CE .NET, see theWindows CE .NET Start Site

For online d0cumentation and context-sensitive Help included with Windows CE .NET, see the Windows CE .NET product d0cumentation.

LIST