Click here to Skip to main content
15,893,588 members
Articles / Programming Languages / C#

Windows Mobile Programming Tricks on the .NET Compact Framework: Part 1

Rate me:
Please Sign up or sign in to vote.
4.64/5 (8 votes)
23 Feb 2011CPOL4 min read 41.1K   668   24  
The article provides and describes some useful code snippets for Windows Mobile/CE developers.
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace BeeMobile
{
    public class NativeFileSystem
    {
        const int MAX_PATH = 50;
        static bool m_sbExceptionsEnabled;

        [DllImport("coredll.dll", SetLastError = true, EntryPoint = "SHGetSpecialFolderPath")]
        static extern bool SHGetSpecialFolderPath(int hwndOwner, string lpszPath, Folders nFolder, bool fCreate);

        NativeFileSystem()
        { }

        public static string GetFolderPath(Folders folder)
        {
            string sPath = new string(' ', MAX_PATH);
            bool bRet;

            try
            {
                bRet = SHGetSpecialFolderPath(0, sPath, folder, false);
            }
            catch (Exception ex)
            {
                HandleCeError(ex, "GetFolderPath");
                return null;
            }

            if (!bRet)
            {
                int errorNum = Marshal.GetLastWin32Error();
                HandleCeError(new WinCeException("SHGetSpecialFolderPath returned false, likely an invalid constant", errorNum), "GetSpecialFolderPath");
            }

            return sPath;
        }

        private static void HandleCeError(Exception ex, string method)
        {
            // logging

            if (!ExceptionsEnabled)
            {
                return;
            }

            if (ex is NotSupportedException)
            {
                throw new WinCeException("Bad arguments or incorrect declaration in " + method, 0, ex);
            }

            if (ex is MissingMethodException)
            {
                throw new WinCeException("Entry point not found in " + method, 0, ex);
            }

            if (ex is WinCeException)
            {
                throw ex;
            }

            throw new WinCeException("Miscellaneous exception in " + method, 0, ex);
        }


        #region Properties
        public static bool ExceptionsEnabled
        {
            get
            {
                return m_sbExceptionsEnabled;
            }
            set
            {
                m_sbExceptionsEnabled = value;
            }
        }
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
G&M Dynamics, s.r.o.
Slovakia Slovakia
I work for Bee Mobile.

Web site: http://beemobile4.net
Facebook site: http://facebook.com/BeeMobile
YouTube Channel: http://youtube.com/beemobile4dotnet

Comments and Discussions