コード例 #1
0
        public static void SetFileCreationTime(
            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.SetFileTime1(handle.DangerousGetHandle(), ref d, IntPtr.Zero, IntPtr.Zero))
                    {
                        var lastWin32Error = Marshal.GetLastWin32Error();
                        throw new Win32Exception(
                                  lastWin32Error,
                                  string.Format(
                                      Resources.ErrorSettingCreationTime,
                                      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.SetCreationTime(filePath, date);
                }
                else if (Directory.Exists(filePath))
                {
                    Directory.SetCreationTime(filePath, date);
                }
                else
                {
                    throw new FileNotFoundException(Resources.FileNotFound, filePath);
                }
            }
        }