C#文件创建、修改、访问时间修改

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;

using System.Windows.Forms;
using System.IO;

namespace 修改文件创建日期或修改日期
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string path = "";
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "请选择要打开的文件";
            ofd.Multiselect = false;
            //ofd.InitialDirectory = @"C:\Uss\shaofeng\Desktop";
            ofd.Filter = "文本文件|*.txt|所有文件|*.*";
            ofd.ShowDialog();
            path = ofd.FileName;
            if (path=="")
            {
                return;
            }
            textBox1.Text = Path.GetFileName( path);

            FileInfo file = new FileInfo(path);
            textBox2.Text = "创建时间:" + file.CreationTime.ToString() + "\r\n修改时间:" + file.LastWriteTime.ToString() + "\r\n访问时间:" + file.LastAccessTime.ToString();


        }

        private void button2_Click(object sender, EventArgs e)
        {
            string strCuang = tboCuang.Text;
           
            string strXou = tboXou.Text;
            
            string strFw = tboFw.Text;
            if (path=="")
            {
                return;
            }
            FileInfo file = new FileInfo(path);
            try
            {
                int[] cj = strToNumTime(strCuang);
                int[] xg = strToNumTime(strXou);
                int[] fw = strToNumTime(strFw);

                file.CreationTime = new DateTime(cj[0], cj[1], cj[2], cj[3], cj[4], cj[5]);
                file.LastWriteTime = new DateTime(xg[0], xg[1], xg[2], xg[3], xg[4], xg[5]);
                file.LastAccessTime = new DateTime(fw[0], fw[1], fw[2], fw[3], fw[4], fw[5]);
               
            }
            catch 
            {

               
            }
            finally
            {
                textBox3.Text = "创建时间:" + file.CreationTime.ToString() + "\r\n修改时间:" + file.LastWriteTime.ToString() + "\r\n访问时间:" + file.LastAccessTime.ToString();
            }

        }

        private static int[] strToNumTime(string str)
        {
            string[] cjTime = str.Split(new char[] { ' ', '/', ':' }, StringSplitOptions.RemoveEmptyEntries);
            int[] cj = new int[cjTime.Length];
            for (int i = 0; i < cjTime.Length; i++)
            {
                cj[i] = Convert.ToInt32(cjTime[i]);
            }

            return cj;
        }
    }
}

  

转载于:https://www.cnblogs.com/bcd589/p/6258363.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值