Esempio n. 1
0
        public static void SetFileLastWriteTime(
            string filePath,
            DateTime date)
        {
            if (MustBeLongPath(filePath))
            {
                filePath = CheckAddLongPathPrefix(filePath);

                using (var handle = CreateFileHandle(
                           filePath,
                           CreationDisposition.OpenExisting,
                           FileAccess.GenericWrite,
                           FileShare.Read | FileShare.Write))
                {
                    var d = date.ToFileTime();

                    if (!PInvokeHelper.SetFileTime3(handle.DangerousGetHandle(), IntPtr.Zero, IntPtr.Zero, ref d))
                    {
                        var lastWin32Error = Marshal.GetLastWin32Error();
                        throw new Win32Exception(
                                  lastWin32Error,
                                  string.Format(
                                      Resources.ErrorSettingsWriteTime,
                                      lastWin32Error,
                                      filePath,
                                      CheckAddDotEnd(new Win32Exception(lastWin32Error).Message)));
                    }
                }
            }
            else
            {
                // 2012-08-29, Uwe Keim: Since we currently get Access Denied 5,
                // do use the .NET functions (which seem to not have these issues)
                // if possible.

                // Sergey Filippov: Item number 16314 from Codeplex issues fix
                if (File.Exists(filePath))
                {
                    File.SetLastWriteTime(filePath, date);
                }
                else if (Directory.Exists(filePath))
                {
                    Directory.SetLastWriteTime(filePath, date);
                }
                else
                {
                    throw new FileNotFoundException(Resources.FileNotFound, filePath);
                }
            }
        }