file360

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
/**********************************************************************
 * VLC for WinRT
 **********************************************************************
 * Copyright © 2013-2014 VideoLAN and Authors
 *
 * Licensed under GPLv2+ and MPLv2
 * Refer to COPYING file of the official project for license
 **********************************************************************/

using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using Windows.Storage;

namespace File360.Utility.Helpers
{
    internal class NativeOperationsHelper
    {
        // ReSharper disable InconsistentNaming
        /// <summary>
        /// La déclaration de l'API GetFileAttributesEx
        /// </summary>
        /// <param name="lpFileName">Le nom du fichier recherché</param>
        /// <param name="fInfoLevelId">Quelles types d'info on recherche</param>
        /// <param name="lpFileInformation">Structure contenant les informations une fois l'API appelée</param>
        /// <returns></returns>
        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetFileAttributesEx(
            string lpFileName,
            GET_FILEEX_INFO_LEVELS fInfoLevelId,
            out WIN32_FILE_ATTRIBUTE_DATA lpFileInformation);

        /// <summary>
        /// Type d'infos recherchées
        /// </summary>
        public enum GET_FILEEX_INFO_LEVELS
        {
            /// <summary>
            /// C'est la seule valeur autorisée avec WinRT
            /// </summary>
            GetFileExInfoStandard,
            /// <summary>
            /// Ne fonctionne pas sous WinRT => marqué Obsolete
            /// </summary>
            [Obsolete]
            GetFileExMaxInfoLevel
        }

        /// <summary>
        /// Structure contenant les informations sur le fichier
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct WIN32_FILE_ATTRIBUTE_DATA
        {
            public FileAttributes dwFileAttributes;
            public FILETIME ftCreationTime;
            public FILETIME ftLastAccessTime;
            public FILETIME ftLastWriteTime;
            public uint nFileSizeHigh;
            public uint nFileSizeLow;
        }

        // quelques valeurs d'erreurs succeptibles d'arriver
        private const int ERROR_FILE_NOT_FOUND = 2;
        private const int ERROR_PATH_NOT_FOUND = 3;
        private const int ERROR_ACCESS_DENIED = 5;

        public static bool FileExist(string fileName)
        {
            WIN32_FILE_ATTRIBUTE_DATA fileData;
            if (GetFileAttributesEx(fileName, GET_FILEEX_INFO_LEVELS.GetFileExInfoStandard, out fileData))
                return true;

            // récupération de la dernière erreur (in the same thread of course ;-))
            var lastError = Marshal.GetLastWin32Error();
            if (lastError == ERROR_FILE_NOT_FOUND || lastError == ERROR_PATH_NOT_FOUND) return false;
            // si c'est pas un fichier non trouvé, on lance une exception
            if (lastError == ERROR_ACCESS_DENIED)
                throw new SecurityAccessDeniedException("Acces Denied");

            throw new InvalidOperationException(string.Format("Erreur pendant l'accès au fichier {0}, code {1}", fileName, lastError));
        }

    }
}