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


            }
        }
    }

  • 相关阅读:
    SAP S/4HANA extensibility扩展原理介绍
    SAP CRM系统订单模型的设计与实现
    使用nodejs代码在SAP C4C里创建Individual customer
    SAP Cloud for Customer Account和individual customer的区别
    Let the Balloon Rise map一个数组
    How Many Tables 简单并查集
    Heap Operations 优先队列
    Arpa’s obvious problem and Mehrdad’s terrible solution 思维
    Passing the Message 单调栈两次
    The Suspects 并查集
  • 原文地址:https://www.cnblogs.com/upshania/p/1883887.html
Copyright © 2011-2022 走看看