首页 > 代码库 > C#文件创建、修改、访问时间修改
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; } } }
C#文件创建、修改、访问时间修改
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。