0

I have a code written as Windows desktop application. I want to convert it to windows storeapp. However, I could not found equivalent of SafeFileHandle.

Here is the code.

using Microsoft.Win32.Safehandles;
using System;
using System.IO;
using System.Runtime.InteropServices;

internal sealed class FileIO
{
    internal const int FILE_ATTRIBUTE_NORMAL = 0x80;
    internal const int FILE_FLAG_DELETE_ON_CLOSE = 0x4000000;
    internal const int FILE_FLAG_OVERLAPPED = 0x40000000;
    internal const int FILE_FLAG_SEQUENTIAL_SCAN = 0x8000000;
    internal const int FILE_SHARE_DELETE = 4;
    internal const int FILE_SHARE_READ = 1;
    internal const int FILE_SHARE_WRITE = 2;
    internal const uint GENERIC_READ = 0x80000000;
    internal const uint GENERIC_WRITE = 0x40000000;
    internal const int INVALID_HANDLE_VALUE = -1;
    internal const int OPEN_ALWAYS = 4;
    internal const int OPEN_EXISTING = 3;

    [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    internal static extern SafeFileHandle CreateFile(string lpFileName, uint dwDesiredAccess, int dwShareMode, IntPtr lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile);
}

Microsoft.Win32.Safehandles dll is not supported in Windows Storeapp. It gives error "the type or namespace name Win32 does not exist in the namespace 'Microsoft'(are missing an assembly reference?)"

Therefore, SafeFilehandle gives error like that "The type or namespace name 'SafeFileHandle' could not be found".

Do you have any suggestion?

Thanks.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • Any reason for P/Invoking `CreateFile`? Why can't you use `File.Create` or similar helper methods? – Sriram Sakthivel Nov 12 '14 at 09:07
  • @SriramSakthivel working with alternative streams from top of my head. – Michal Hosala Nov 12 '14 at 09:08
  • @MichalHosala My guess too :) But lets see if that's the case.. – Sriram Sakthivel Nov 12 '14 at 09:09
  • 3
    Store apps run on a fundamentally different underlying platform. That has lots of consequences, beyond the removal handle-based resource management, your CreateFile() call can never succeed. It is blocked by the sandbox. File.Create() is unavailable for the same reason. Avoid asking XY questions, always explain what problem you are *really* trying to solve. – Hans Passant Nov 12 '14 at 11:19

0 Answers0