2016-04-16 10 views
1

同じファイルを書き込む2つのプロセスがあります。それはログファイルBTW、アクセスは名前付きミューテックスと同期されます。WinRTのファイル共有モード

どのようにしてStorageFolder.OpenStreamForWriteAsyncを作成しますか?FILE_SHARE_READ | kernelbase.dllからおそらく使用される基本的なCreateFile2 WinAPIにFILE_SHARE_WRITE?

答えて

1

これは不可能です。

回避策 - アンマネージドinteropを使用して独自のFileStreamクラスを実装します。これは比較的単純ですが、私のコードは4ページ以下です。

は、ここでは、そのために必要があります輸入です:

static class NativeMethods 
{ 
    const string file121 = "api-ms-win-core-file-l1-2-1.dll"; 
    const string handle110 = "api-ms-win-core-handle-l1-1-0.dll"; 

    [DllImport(file121, CharSet = CharSet.Unicode, SetLastError = true)] 
    public static extern SafeFileHandle CreateFile2([MarshalAs(UnmanagedType.LPWStr)] string filename, 
     FileAccess dwDesiredAccess, FileShare dwShareMode, FileMode dwCreationDisposition, IntPtr pCreateExParams); 

    [DllImport(handle110, SetLastError = true)] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    internal static extern bool CloseHandle(IntPtr handle); 

    [DllImport(file121, SetLastError = true)] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    static extern bool GetFileInformationByHandleEx(SafeFileHandle handle, FILE_INFO_BY_HANDLE_CLASS FileInformationClass, out FILE_STANDARD_INFO lpFileInformation, int dwBufferSize); 

    internal static FILE_STANDARD_INFO GetFileStandardInfo(SafeFileHandle handle) 
    { 
     FILE_STANDARD_INFO fsi; 
     if(!GetFileInformationByHandleEx(handle, FILE_INFO_BY_HANDLE_CLASS.FileStandardInfo, out fsi, Marshal.SizeOf<FILE_STANDARD_INFO>())) 
      throw new COMException("GetFileInformationByHandleEx failed.", Marshal.GetHRForLastWin32Error()); 
     return fsi; 
    } 

    [DllImport(file121, SetLastError = true)] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    internal static extern bool SetFilePointerEx(SafeFileHandle handle, long liDistanceToMove, out long lpNewFilePointer, SeekOrigin dwMoveMethod); 

    [DllImport(file121, SetLastError = true)] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    internal static extern bool SetFilePointerEx(SafeFileHandle handle, long liDistanceToMove, IntPtr lpNewFilePointer, SeekOrigin dwMoveMethod); 

    [DllImport(file121, SetLastError = true)] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    internal static extern bool FlushFileBuffers(SafeFileHandle handle); 

    [DllImport(file121, SetLastError = true)] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    internal static extern unsafe bool ReadFile(SafeFileHandle hFile, byte* lpBuffer, uint nNumberOfBytesToRead, out uint lpNumberOfBytesRead, IntPtr lpOverlapped); 

    [DllImport(file121, SetLastError = true)] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    internal static extern unsafe bool SetEndOfFile(SafeFileHandle hFile); 

    [ DllImport(file121, SetLastError = true)] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    internal static extern unsafe bool WriteFile(SafeFileHandle hFile, byte* lpBuffer, uint nNumberOfBytesToWrite, out uint lpNumberOfBytesWritten, IntPtr lpOverlapped); } 

例えば、あなたが構造体/列挙型/ SafeFileHandleをも必要になりますが、輸入品とは異なり、これらはペーストをコピーするのは簡単ですCoreFXから。