public static void DeleteLogFile(
     string pszLogFileName,
     SECURITY_ATTRIBUTES psaLogFile)
 {
     if (!_DeleteLogFile(pszLogFileName, psaLogFile))
     {
         uint errorCode = (uint)Marshal.GetLastWin32Error();
         switch (errorCode)
         {
             case Error.ERROR_ACCESS_DENIED:
             case Error.ERROR_FILE_NOT_FOUND:
             case Error.ERROR_INVALID_PARAMETER:
             case Error.ERROR_SHARING_VIOLATION:
             case Error.ERROR_INVALID_NAME:
             case Error.ERROR_BAD_PATHNAME:
             case Error.ERROR_BAD_NETPATH:
                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(Error.ExceptionForKnownCode(errorCode));
             case Error.ERROR_NOT_SUPPORTED:
                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.Argument_FileNameInvalid));
             default:
                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(Error.ExceptionForUnknownCode(errorCode));
         }
     }
 }
        public static SafeFileHandle CreateLogFile(
            string pszLogFileName,
            int fDesiredAccess,
            System.IO.FileShare dwShareMode,
            SECURITY_ATTRIBUTES psaLogFile,
            System.IO.FileMode fCreateDisposition,
            int fFlagsAndAttributes)
        {
            SafeFileHandle ret = _CreateLogFile(
                pszLogFileName,
                fDesiredAccess,
                dwShareMode,
                psaLogFile,
                fCreateDisposition,
                fFlagsAndAttributes);

            if (ret.IsInvalid)
            {
                uint errorCode = (uint)Marshal.GetLastWin32Error();
                switch (errorCode)
                {
                    case Error.ERROR_SHARING_VIOLATION:
                    case Error.ERROR_ALREADY_EXISTS:
                    case Error.ERROR_ACCESS_DENIED:
                    case Error.ERROR_FILE_NOT_FOUND:
                    case Error.ERROR_PATH_NOT_FOUND:
                    case Error.ERROR_NO_SYSTEM_RESOURCES:
                    case Error.ERROR_OUTOFMEMORY:
                    case Error.ERROR_INVALID_PARAMETER:
                    case Error.ERROR_INVALID_NAME:
                    case Error.ERROR_BAD_PATHNAME:
                    case Error.ERROR_BAD_NETPATH:
                    case Error.ERROR_CANT_RESOLVE_FILENAME:
                    case Error.ERROR_LOG_BLOCK_VERSION:
                    case Error.ERROR_LOG_BLOCK_INVALID:
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(Error.ExceptionForKnownCode(errorCode));
                    case Error.ERROR_NOT_SUPPORTED:
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.Argument_FileNameInvalid));
                    default:
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(Error.ExceptionForUnknownCode(errorCode));
                }
            }

            return ret;
        }
 extern static SafeFileHandle _CreateLogFile(
     string pszLogFileName,
     int fDesiredAccess,
     System.IO.FileShare dwShareMode,
     SECURITY_ATTRIBUTES psaLogFile,
     System.IO.FileMode fCreateDisposition,
     int fFlagsAndAttributes);
 extern static bool _DeleteLogFile(
     string pszLogFileName,
     SECURITY_ATTRIBUTES psaSecurityAttributes);
Exemple #5
0
        // If pinningHandle is not null, caller must free it AFTER the call to
        // CreateFile has returned.
        //
        // [....]: Copied this from System.IO.FileStream
        //
        private static unsafe SECURITY_ATTRIBUTES GetSecAttrs(
            FileShare share,
            FileSecurity fileSecurity,
            out object pinningHandle)
        {
            pinningHandle = null;
            SECURITY_ATTRIBUTES secAttrs = null;
            if ((share & FileShare.Inheritable) != 0 || fileSecurity != null)
            {
                secAttrs = new SECURITY_ATTRIBUTES();
                secAttrs.nLength = (int)Marshal.SizeOf(secAttrs);

                if ((share & FileShare.Inheritable) != 0)
                {
                    secAttrs.bInheritHandle = 1;
                }

                // For ACLs, get the security descriptor from the FileSecurity.
                if (fileSecurity != null)
                {
                    byte[] sd = fileSecurity.GetSecurityDescriptorBinaryForm();
                    pinningHandle = GCHandle.Alloc(sd, GCHandleType.Pinned);
                    fixed (byte* pSecDescriptor = sd)
                        secAttrs.pSecurityDescriptor = pSecDescriptor;
                }
            }
            return secAttrs;
        }