Exemple #1
0
        // Token: 0x06000CAC RID: 3244 RVA: 0x0006EAE4 File Offset: 0x0006CCE4
        public static TempFileStream CreateInstance(string prefix, bool deleteOnClose)
        {
            NativeMethods.SecurityAttributes securityAttributes = new NativeMethods.SecurityAttributes(false);
            string path = TempFileStream.Path;

            new FileIOPermission(FileIOPermissionAccess.Write, path).Demand();
            int            num  = 0;
            int            num2 = 10;
            string         filename;
            SafeFileHandle safeFileHandle;

            for (;;)
            {
                filename = System.IO.Path.Combine(path, prefix + ((uint)Interlocked.Increment(ref TempFileStream.nextId)).ToString("X5") + ".tmp");
                uint num3 = deleteOnClose ? 67108864U : 0U;
                safeFileHandle = NativeMethods.CreateFile(filename, 1180063U, 0U, ref securityAttributes, 1U, 256U | num3 | 8192U, IntPtr.Zero);
                num2--;
                if (safeFileHandle.IsInvalid)
                {
                    num = Marshal.GetLastWin32Error();
                    if (num == 80)
                    {
                        num2++;
                    }
                    using (Process currentProcess = Process.GetCurrentProcess())
                    {
                        Interlocked.Add(ref TempFileStream.nextId, currentProcess.Id);
                        goto IL_CB;
                    }
                    goto IL_C8;
                }
                goto IL_C8;
IL_CB:
                if (num2 <= 0)
                {
                    break;
                }
                continue;
IL_C8:
                num2 = 0;
                goto IL_CB;
            }
            if (safeFileHandle.IsInvalid)
            {
                string message = SharedStrings.CreateFileFailed(filename);
                throw new IOException(message, new Win32Exception(num, message));
            }
            return(new TempFileStream(safeFileHandle)
            {
                filePath = filename
            });
        }
 internal static extern SafeFileHandle CreateFile([In] string filename, [In] uint accessMode, [In] uint shareMode, ref NativeMethods.SecurityAttributes securityAttributes, [In] uint creationDisposition, [In] uint flags, [In] IntPtr templateFileHandle);
Exemple #3
0
        public static TempFileStream CreateInstance(string prefix, bool deleteOnClose)
        {
            var securityAttribute = new NativeMethods.SecurityAttributes(false);

            var tempPath = Path;



            new FileIOPermission(FileIOPermissionAccess.Write, tempPath).Demand();

            SafeFileHandle safeHandle;
            string         tempFile;
            var            errorCode = 0;


            var retry = 10;

            do
            {
                var id = (uint)Interlocked.Increment(ref NextId);

                tempFile =
                    System.IO.Path.Combine(
                        tempPath,
                        string.Concat(prefix, id.ToString("X5"), ".tmp"));

                var deleteOnCloseFlag = (deleteOnClose) ? NativeMethods.FILE_FLAG_DELETE_ON_CLOSE : 0;
                safeHandle = NativeMethods.CreateFile(
                    tempFile,
                    NativeMethods.FILE_GENERIC_READ | NativeMethods.FILE_GENERIC_WRITE,
                    0,
                    ref securityAttribute,
                    NativeMethods.CREATE_NEW,
                    NativeMethods.FILE_ATTRIBUTE_TEMPORARY | deleteOnCloseFlag | NativeMethods.FILE_ATTRIBUTE_NOT_CONTENT_INDEXED,
                    IntPtr.Zero);

                retry--;

                if (safeHandle.IsInvalid)
                {
                    errorCode = Marshal.GetLastWin32Error();

                    if (errorCode == NativeMethods.ERROR_FILE_EXISTS)
                    {
                        retry++;
                    }


                    Interlocked.Add(ref NextId, Process.GetCurrentProcess().Id);
                }
                else
                {
                    retry = 0;
                }
            }while (retry > 0);

            if (safeHandle.IsInvalid)
            {
                var message = Strings.CreateFileFailed(tempFile);
                throw new IOException(message, new Win32Exception(errorCode, message));
            }

            var tempFileStream = new TempFileStream(safeHandle);

            tempFileStream.filePath = tempFile;
            return(tempFileStream);
        }