Exemplo n.º 1
0
        public static void DeleteFileAfterReboot(
            string sourceFilePath,
            bool throwIfFails = false)
        {
            // http://stackoverflow.com/questions/6077869/movefile-function-in-c-sharp-delete-file-after-reboot-c-sharp

            sourceFilePath = CheckAddLongPathPrefix(sourceFilePath);

            // Aus der Doku:
            // "...This value can be used only if the process is in the context of a user who belongs to the administrators group or the LocalSystem account..."
            if (!PInvokeHelper.MoveFileEx(sourceFilePath, null, 4))
            {
                var lastWin32Error = Marshal.GetLastWin32Error();

                if (throwIfFails)
                {
                    // http://msdn.microsoft.com/en-us/library/ms681382(VS.85).aspx.

                    throw new Win32Exception(
                              lastWin32Error,
                              string.Format(
                                  Resources.ErrorMarkingFileForDeletion,
                                  lastWin32Error,
                                  sourceFilePath,
                                  CheckAddDotEnd(new Win32Exception(lastWin32Error).Message)));
                }
                else
                {
                    Trace.TraceWarning(@"Error {0} marking file '{1}' for deletion after reboot: {2}",
                                       lastWin32Error,
                                       sourceFilePath,
                                       CheckAddDotEnd(new Win32Exception(lastWin32Error).Message));
                }
            }
        }