zoukankan      html  css  js  c++  java
  • 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:UssshaofengDesktop";
                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() + "
    修改时间:" + file.LastWriteTime.ToString() + "
    访问时间:" + 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() + "
    修改时间:" + file.LastWriteTime.ToString() + "
    访问时间:" + 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;
            }
        }
    }
    

      

  • 相关阅读:
    MySQL my.cnf详解
    函数:sleep-exit-wait
    fork-小实验
    OS-进程调度
    CET-4流程
    SDK和API的区别
    生活-金钱管理-不是理财
    算法设计与分析:Strassen矩阵乘法
    力扣:二进制加法求和
    算法设计与分析:大整数乘法
  • 原文地址:https://www.cnblogs.com/bcd589/p/6258363.html
Copyright © 2011-2022 走看看