zoukankan      html  css  js  c++  java
  • 指定某个文件的创建 修改 访问时间

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.IO;

    namespace SetFileSystem
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            [DllImport("kernel32.dll", SetLastError = true)]
            [return: MarshalAs(UnmanagedType.Bool)]
            static extern bool SetFileTime(SafeHandle hFile, ref long lpCreationTime, ref long lpLastAccessTime, ref long lpLastWriteTime);

            public static void SetFileTimes(SafeHandle hFile, DateTime creationTime, DateTime accessTime, DateTime writeTime)
            {
                long lCreationTime = creationTime.ToFileTime();
                long lAccessTime = accessTime.ToFileTime();
                long lWriteTime = writeTime.ToFileTime();

                if (!SetFileTime(hFile, ref lCreationTime, ref lAccessTime, ref lWriteTime))
                {
                    throw new Win32Exception();
                }
            }

            private void button1_Click(object sender, EventArgs e)
            {
                Microsoft.Win32.SafeHandles.SafeFileHandle handle;
                string path = "c:\\a.txt";
                FileStream stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite, 1);
                handle = stream.SafeFileHandle;
                if (!handle.IsInvalid)
                {
                    DateTime dn = DateTime.Now;
                    long dt = dn.ToFileTime();

                    if (SetFileTime(handle, ref dt, ref dt, ref dt))
                    {
                        MessageBox.Show("成功!");
                    }
                    else
                    {
                        MessageBox.Show("失败!");
                    }
                }
                stream.Close();


            }
        }
    }

  • 相关阅读:
    centos6升级内核
    centos7启动盘制作
    sed匹配字符串并将匹配行的后几行注释
    cmd下调用xshell登录linux服务器
    centos清除历史命令
    yum错误Cannot retrieve metalink for repository: epel/x86_6
    ansible-playbook
    jdk升级到1.8
    shell瞎记录
    shell 循环
  • 原文地址:https://www.cnblogs.com/upshania/p/1883887.html
Copyright © 2011-2022 走看看