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();


            }
        }
    }

  • 相关阅读:
    Appdelegate 跳转其他页面 获取当前屏幕显示的viewcontroller 获取当前屏幕中present出来的viewcontroller 获取当前导航控制器
    React-Native 环境部署
    关于GCD的那些事
    二,Runtime进行动态添加方法
    一, Runtime 交换方法
    Runtime 概念
    Mac Office安装及破解
    iOS 规范之宏
    规范之UITableViewCell
    Linux 命令
  • 原文地址:https://www.cnblogs.com/upshania/p/1883887.html
Copyright © 2011-2022 走看看