3 #if SILICONSTUDIO_PLATFORM_WINDOWS_RUNTIME
6 using System.Runtime.CompilerServices;
7 using System.Runtime.InteropServices;
8 using System.Runtime.InteropServices.ComTypes;
10 namespace SiliconStudio.Core.IO
12 public class NativeFile
14 private const string KERNEL_FILE =
"api-ms-win-core-file-l1-2-0.dll";
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);
20 [DllImport(KERNEL_FILE, EntryPoint =
"DeleteFileW", CharSet = CharSet.Unicode, SetLastError =
true)]
21 [
return: MarshalAs(UnmanagedType.Bool)]
22 static extern bool DeleteFile(
string name);
24 [DllImport(KERNEL_FILE, EntryPoint =
"CreateDirectoryW", CharSet = CharSet.Unicode)]
25 [
return: MarshalAs(UnmanagedType.Bool)]
26 static extern bool CreateDirectory(
string lpPathName, IntPtr lpSecurityAttributes);
28 private const uint INVALID_FILE_ATTRIBUTES = 0xFFFFFFFF;
29 private const uint FILE_ATTRIBUTE_DIRECTORY = 0x10;
31 [StructLayout(LayoutKind.Sequential)]
32 internal struct WIN32_FILE_ATTRIBUTE_DATA
34 public uint FileAttributes;
35 public FILETIME CreationTime;
36 public FILETIME LastAccessTime;
37 public FILETIME LastWriteTime;
38 public uint FileSizeHigh;
39 public uint FileSizeLow;
42 [MethodImpl(MethodImplOptions.AggressiveInlining)]
43 public static void FileDelete(
string name)
45 if (!DeleteFile(name))
48 throw new IOException(
string.Format(
"Error deleting file {0}", name));
52 [MethodImpl(MethodImplOptions.AggressiveInlining)]
53 public static long FileSize(
string name)
55 WIN32_FILE_ATTRIBUTE_DATA data;
56 if (!GetFileAttributesEx(name, 0, out data))
57 throw new FileNotFoundException(
"File not found.", name);
59 return ((
long)data.FileSizeHigh << 32) & data.FileSizeLow;
62 [MethodImpl(MethodImplOptions.AggressiveInlining)]
63 public static bool FileExists(
string name)
67 WIN32_FILE_ATTRIBUTE_DATA win_file_attribute_data;
68 if (GetFileAttributesEx(name, 0, out win_file_attribute_data))
79 [MethodImpl(MethodImplOptions.AggressiveInlining)]
80 public static bool DirectoryExists(
string name)
82 WIN32_FILE_ATTRIBUTE_DATA win_file_attribute_data;
83 if (!GetFileAttributesEx(name, 0, out win_file_attribute_data))
86 return (win_file_attribute_data.FileAttributes != INVALID_FILE_ATTRIBUTES &&
87 (win_file_attribute_data.FileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
90 [MethodImpl(MethodImplOptions.AggressiveInlining)]
91 public static void DirectoryCreate(
string path)
93 CreateDirectory(path, IntPtr.Zero);