码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2017-01-07 08:19:12      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:win   http   lte   last   文件创建   ace   .com   oid   har   

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

  技术分享

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

标签:win   http   lte   last   文件创建   ace   .com   oid   har   

原文地址:http://www.cnblogs.com/bcd589/p/6258363.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!