file360

Log | Files | Refs

NativeOperationsHelper.cs (3397B)


      1 /**********************************************************************
      2  * VLC for WinRT
      3  **********************************************************************
      4  * Copyright © 2013-2014 VideoLAN and Authors
      5  *
      6  * Licensed under GPLv2+ and MPLv2
      7  * Refer to COPYING file of the official project for license
      8  **********************************************************************/
      9 
     10 using System;
     11 using System.Runtime.InteropServices;
     12 using System.Runtime.InteropServices.ComTypes;
     13 using Windows.Storage;
     14 
     15 namespace File360.Utility.Helpers
     16 {
     17     internal class NativeOperationsHelper
     18     {
     19         // ReSharper disable InconsistentNaming
     20         /// <summary>
     21         /// La déclaration de l'API GetFileAttributesEx
     22         /// </summary>
     23         /// <param name="lpFileName">Le nom du fichier recherché</param>
     24         /// <param name="fInfoLevelId">Quelles types d'info on recherche</param>
     25         /// <param name="lpFileInformation">Structure contenant les informations une fois l'API appelée</param>
     26         /// <returns></returns>
     27         [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
     28         [return: MarshalAs(UnmanagedType.Bool)]
     29         public static extern bool GetFileAttributesEx(
     30             string lpFileName,
     31             GET_FILEEX_INFO_LEVELS fInfoLevelId,
     32             out WIN32_FILE_ATTRIBUTE_DATA lpFileInformation);
     33 
     34         /// <summary>
     35         /// Type d'infos recherchées
     36         /// </summary>
     37         public enum GET_FILEEX_INFO_LEVELS
     38         {
     39             /// <summary>
     40             /// C'est la seule valeur autorisée avec WinRT
     41             /// </summary>
     42             GetFileExInfoStandard,
     43             /// <summary>
     44             /// Ne fonctionne pas sous WinRT => marqué Obsolete
     45             /// </summary>
     46             [Obsolete]
     47             GetFileExMaxInfoLevel
     48         }
     49 
     50         /// <summary>
     51         /// Structure contenant les informations sur le fichier
     52         /// </summary>
     53         [StructLayout(LayoutKind.Sequential)]
     54         public struct WIN32_FILE_ATTRIBUTE_DATA
     55         {
     56             public FileAttributes dwFileAttributes;
     57             public FILETIME ftCreationTime;
     58             public FILETIME ftLastAccessTime;
     59             public FILETIME ftLastWriteTime;
     60             public uint nFileSizeHigh;
     61             public uint nFileSizeLow;
     62         }
     63 
     64         // quelques valeurs d'erreurs succeptibles d'arriver
     65         private const int ERROR_FILE_NOT_FOUND = 2;
     66         private const int ERROR_PATH_NOT_FOUND = 3;
     67         private const int ERROR_ACCESS_DENIED = 5;
     68 
     69         public static bool FileExist(string fileName)
     70         {
     71             WIN32_FILE_ATTRIBUTE_DATA fileData;
     72             if (GetFileAttributesEx(fileName, GET_FILEEX_INFO_LEVELS.GetFileExInfoStandard, out fileData))
     73                 return true;
     74 
     75             // récupération de la dernière erreur (in the same thread of course ;-))
     76             var lastError = Marshal.GetLastWin32Error();
     77             if (lastError == ERROR_FILE_NOT_FOUND || lastError == ERROR_PATH_NOT_FOUND) return false;
     78             // si c'est pas un fichier non trouvé, on lance une exception
     79             if (lastError == ERROR_ACCESS_DENIED)
     80                 throw new SecurityAccessDeniedException("Acces Denied");
     81 
     82             throw new InvalidOperationException(string.Format("Erreur pendant l'accès au fichier {0}, code {1}", fileName, lastError));
     83         }
     84 
     85     }
     86 }