Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
NativeFile.WindowsRuntime.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 #if SILICONSTUDIO_PLATFORM_WINDOWS_RUNTIME
4 using System;
5 using System.IO;
6 using System.Runtime.CompilerServices;
7 using System.Runtime.InteropServices;
8 using System.Runtime.InteropServices.ComTypes;
9 
10 namespace SiliconStudio.Core.IO
11 {
12  public class NativeFile
13  {
14  private const string KERNEL_FILE = "api-ms-win-core-file-l1-2-0.dll";
15 
16  [DllImport(KERNEL_FILE, EntryPoint = "GetFileAttributesExW", CharSet = CharSet.Unicode, SetLastError = true)]
17  [return: MarshalAs(UnmanagedType.Bool)]
18  static extern bool GetFileAttributesEx(string name, int fileInfoLevel, out WIN32_FILE_ATTRIBUTE_DATA lpFileInformation);
19 
20  [DllImport(KERNEL_FILE, EntryPoint = "DeleteFileW", CharSet = CharSet.Unicode, SetLastError = true)]
21  [return: MarshalAs(UnmanagedType.Bool)]
22  static extern bool DeleteFile(string name);
23 
24  [DllImport(KERNEL_FILE, EntryPoint = "CreateDirectoryW", CharSet = CharSet.Unicode)]
25  [return: MarshalAs(UnmanagedType.Bool)]
26  static extern bool CreateDirectory(string lpPathName, IntPtr lpSecurityAttributes);
27 
28  private const uint INVALID_FILE_ATTRIBUTES = 0xFFFFFFFF;
29  private const uint FILE_ATTRIBUTE_DIRECTORY = 0x10;
30 
31  [StructLayout(LayoutKind.Sequential)]
32  internal struct WIN32_FILE_ATTRIBUTE_DATA
33  {
34  public uint FileAttributes;
35  public FILETIME CreationTime;
36  public FILETIME LastAccessTime;
37  public FILETIME LastWriteTime;
38  public uint FileSizeHigh;
39  public uint FileSizeLow;
40  }
41 
42  [MethodImpl(MethodImplOptions.AggressiveInlining)]
43  public static void FileDelete(string name)
44  {
45  if (!DeleteFile(name))
46  {
47  // TODO: Process GetLastError() code.
48  throw new IOException(string.Format("Error deleting file {0}", name));
49  }
50  }
51 
52  [MethodImpl(MethodImplOptions.AggressiveInlining)]
53  public static long FileSize(string name)
54  {
55  WIN32_FILE_ATTRIBUTE_DATA data;
56  if (!GetFileAttributesEx(name, 0, out data))
57  throw new FileNotFoundException("File not found.", name);
58 
59  return ((long)data.FileSizeHigh << 32) & data.FileSizeLow;
60  }
61 
62  [MethodImpl(MethodImplOptions.AggressiveInlining)]
63  public static bool FileExists(string name)
64  {
65  try
66  {
67  WIN32_FILE_ATTRIBUTE_DATA win_file_attribute_data;
68  if (GetFileAttributesEx(name, 0, out win_file_attribute_data))
69  {
70  return true;
71  }
72  }
73  catch
74  {
75  }
76  return false;
77  }
78 
79  [MethodImpl(MethodImplOptions.AggressiveInlining)]
80  public static bool DirectoryExists(string name)
81  {
82  WIN32_FILE_ATTRIBUTE_DATA win_file_attribute_data;
83  if (!GetFileAttributesEx(name, 0, out win_file_attribute_data))
84  return false;
85 
86  return (win_file_attribute_data.FileAttributes != INVALID_FILE_ATTRIBUTES &&
87  (win_file_attribute_data.FileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
88  }
89 
90  [MethodImpl(MethodImplOptions.AggressiveInlining)]
91  public static void DirectoryCreate(string path)
92  {
93  CreateDirectory(path, IntPtr.Zero);
94  }
95  }
96 }
97 #endif