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;
        }
    }
}

  

posted @ 2017-01-07 03:13  峰圣榜  阅读(6380)  评论(0编辑  收藏  举报