Application simple pour modifier les attributs d'un fichier (date de création, de modification, d'accès)

Description

Tout est dit dans le titre, c'est une première version donc surement incomplète et à améliorer.

Source / Exemple :


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace FilesManager
{
	/* Available actions : modify created, last modified and last accessed dates */
	public enum ACTION : int
	{
		CREATED		= 1,
		MODIFIED	= 2,
		ACCESSED	= 3
	}
	/// <summary>
	/// A simple windows form application to update file attributes
	/// </summary>
	public class FilesManagerUI : System.Windows.Forms.Form
	{
		/* date format used */
		private const string FORMATDATE = "dddd dd MMMM yyyy, hh:mm:ss";
		
		/* all dates used */
		private DateTime dCreated = DateTime.Now;
		private DateTime dAccessed = DateTime.Now;
		private DateTime dModified = DateTime.Now;

		/* file path */
		private string sPath;

		/* action selected by the user */
		private ACTION action;

		private System.Windows.Forms.Label lblAccessed;
		private System.Windows.Forms.Label lblModified;
		private System.Windows.Forms.Label lblCreated;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label lblModifiedDate;
		private System.Windows.Forms.Label lblAccessedDate;
		private System.Windows.Forms.Label lblCreatedDate;
		private System.Windows.Forms.NumericUpDown Date;
		private System.Windows.Forms.NumericUpDown Month;
		private System.Windows.Forms.NumericUpDown Year;
		private System.Windows.Forms.RadioButton Modified;
		private System.Windows.Forms.RadioButton Accessed;
		private System.Windows.Forms.RadioButton FCreated;
		private System.Windows.Forms.Button bModify;
		private System.Windows.Forms.Button bQuit;
		private System.Windows.Forms.Button bBrowse;
		private System.Windows.Forms.StatusBar statusBar;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public FilesManagerUI()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.bBrowse = new System.Windows.Forms.Button();
			this.lblAccessed = new System.Windows.Forms.Label();
			this.lblModified = new System.Windows.Forms.Label();
			this.lblCreated = new System.Windows.Forms.Label();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.lblCreatedDate = new System.Windows.Forms.Label();
			this.lblModifiedDate = new System.Windows.Forms.Label();
			this.lblAccessedDate = new System.Windows.Forms.Label();
			this.Date = new System.Windows.Forms.NumericUpDown();
			this.Month = new System.Windows.Forms.NumericUpDown();
			this.Year = new System.Windows.Forms.NumericUpDown();
			this.Modified = new System.Windows.Forms.RadioButton();
			this.Accessed = new System.Windows.Forms.RadioButton();
			this.FCreated = new System.Windows.Forms.RadioButton();
			this.bModify = new System.Windows.Forms.Button();
			this.bQuit = new System.Windows.Forms.Button();
			this.statusBar = new System.Windows.Forms.StatusBar();
			this.groupBox1.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.Date)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.Month)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.Year)).BeginInit();
			this.SuspendLayout();
			// 
			// bBrowse
			// 
			this.bBrowse.Location = new System.Drawing.Point(16, 216);
			this.bBrowse.Name = "bBrowse";
			this.bBrowse.TabIndex = 0;
			this.bBrowse.Text = "&Browse";
			this.bBrowse.Click += new System.EventHandler(this.bBrowse_Click);
			// 
			// lblAccessed
			// 
			this.lblAccessed.Location = new System.Drawing.Point(16, 24);
			this.lblAccessed.Name = "lblAccessed";
			this.lblAccessed.Size = new System.Drawing.Size(64, 23);
			this.lblAccessed.TabIndex = 1;
			this.lblAccessed.Text = "Accessed :";
			// 
			// lblModified
			// 
			this.lblModified.Location = new System.Drawing.Point(16, 56);
			this.lblModified.Name = "lblModified";
			this.lblModified.Size = new System.Drawing.Size(64, 23);
			this.lblModified.TabIndex = 2;
			this.lblModified.Text = "Modified :";
			// 
			// lblCreated
			// 
			this.lblCreated.Location = new System.Drawing.Point(16, 88);
			this.lblCreated.Name = "lblCreated";
			this.lblCreated.Size = new System.Drawing.Size(64, 23);
			this.lblCreated.TabIndex = 3;
			this.lblCreated.Text = "Created :";
			// 
			// groupBox1
			// 
			this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.groupBox1.Controls.Add(this.lblCreatedDate);
			this.groupBox1.Controls.Add(this.lblModifiedDate);
			this.groupBox1.Controls.Add(this.lblAccessedDate);
			this.groupBox1.Controls.Add(this.lblAccessed);
			this.groupBox1.Controls.Add(this.lblCreated);
			this.groupBox1.Controls.Add(this.lblModified);
			this.groupBox1.Location = new System.Drawing.Point(16, 48);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(368, 128);
			this.groupBox1.TabIndex = 4;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Files Attributes";
			// 
			// lblCreatedDate
			// 
			this.lblCreatedDate.Location = new System.Drawing.Point(96, 88);
			this.lblCreatedDate.Name = "lblCreatedDate";
			this.lblCreatedDate.Size = new System.Drawing.Size(264, 23);
			this.lblCreatedDate.TabIndex = 6;
			// 
			// lblModifiedDate
			// 
			this.lblModifiedDate.Location = new System.Drawing.Point(96, 56);
			this.lblModifiedDate.Name = "lblModifiedDate";
			this.lblModifiedDate.Size = new System.Drawing.Size(264, 23);
			this.lblModifiedDate.TabIndex = 5;
			// 
			// lblAccessedDate
			// 
			this.lblAccessedDate.Location = new System.Drawing.Point(96, 24);
			this.lblAccessedDate.Name = "lblAccessedDate";
			this.lblAccessedDate.Size = new System.Drawing.Size(264, 23);
			this.lblAccessedDate.TabIndex = 4;
			// 
			// Date
			// 
			this.Date.Location = new System.Drawing.Point(16, 184);
			this.Date.Maximum = new System.Decimal(new int[] {
																 31,
																 0,
																 0,
																 0});
			this.Date.Name = "Date";
			this.Date.Size = new System.Drawing.Size(48, 20);
			this.Date.TabIndex = 5;
			// 
			// Month
			// 
			this.Month.Location = new System.Drawing.Point(80, 184);
			this.Month.Maximum = new System.Decimal(new int[] {
																  31,
																  0,
																  0,
																  0});
			this.Month.Name = "Month";
			this.Month.Size = new System.Drawing.Size(48, 20);
			this.Month.TabIndex = 6;
			this.Month.ValueChanged += new System.EventHandler(this.Month_ValueChanged);
			// 
			// Year
			// 
			this.Year.Location = new System.Drawing.Point(144, 184);
			this.Year.Maximum = new System.Decimal(new int[] {
																 2500,
																 0,
																 0,
																 0});
			this.Year.Minimum = new System.Decimal(new int[] {
																 1900,
																 0,
																 0,
																 0});
			this.Year.Name = "Year";
			this.Year.Size = new System.Drawing.Size(48, 20);
			this.Year.TabIndex = 7;
			this.Year.Value = new System.Decimal(new int[] {
															   1900,
															   0,
															   0,
															   0});
			// 
			// Modified
			// 
			this.Modified.Appearance = System.Windows.Forms.Appearance.Button;
			this.Modified.Location = new System.Drawing.Point(128, 8);
			this.Modified.Name = "Modified";
			this.Modified.TabIndex = 9;
			this.Modified.Text = "Modified";
			this.Modified.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			this.Modified.CheckedChanged += new System.EventHandler(this.Type_CheckedChanged);
			// 
			// Accessed
			// 
			this.Accessed.Appearance = System.Windows.Forms.Appearance.Button;
			this.Accessed.Location = new System.Drawing.Point(16, 8);
			this.Accessed.Name = "Accessed";
			this.Accessed.TabIndex = 10;
			this.Accessed.Text = "Accessed";
			this.Accessed.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			this.Accessed.CheckedChanged += new System.EventHandler(this.Type_CheckedChanged);
			// 
			// FCreated
			// 
			this.FCreated.Appearance = System.Windows.Forms.Appearance.Button;
			this.FCreated.Location = new System.Drawing.Point(240, 8);
			this.FCreated.Name = "FCreated";
			this.FCreated.TabIndex = 11;
			this.FCreated.Text = "&Created";
			this.FCreated.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			this.FCreated.CheckedChanged += new System.EventHandler(this.Type_CheckedChanged);
			// 
			// bModify
			// 
			this.bModify.Location = new System.Drawing.Point(104, 216);
			this.bModify.Name = "bModify";
			this.bModify.TabIndex = 12;
			this.bModify.Text = "&Update";
			this.bModify.Click += new System.EventHandler(this.bModify_Click);
			// 
			// bQuit
			// 
			this.bQuit.Location = new System.Drawing.Point(192, 216);
			this.bQuit.Name = "bQuit";
			this.bQuit.TabIndex = 13;
			this.bQuit.Text = "&Quit";
			this.bQuit.Click += new System.EventHandler(this.bQuit_Click);
			// 
			// statusBar
			// 
			this.statusBar.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.statusBar.Location = new System.Drawing.Point(0, 252);
			this.statusBar.Name = "statusBar";
			this.statusBar.Size = new System.Drawing.Size(392, 22);
			this.statusBar.TabIndex = 14;
			// 
			// FilesManagerUI
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(392, 274);
			this.Controls.Add(this.statusBar);
			this.Controls.Add(this.bQuit);
			this.Controls.Add(this.bModify);
			this.Controls.Add(this.FCreated);
			this.Controls.Add(this.Accessed);
			this.Controls.Add(this.Modified);
			this.Controls.Add(this.Year);
			this.Controls.Add(this.Month);
			this.Controls.Add(this.Date);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.bBrowse);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
			this.Name = "FilesManagerUI";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Files Manager";
			this.Load += new System.EventHandler(this.FilesManagerUI_Load);
			this.groupBox1.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.Date)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.Month)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.Year)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new FilesManagerUI());
		}

		private void bBrowse_Click(object sender, System.EventArgs e)
		{
			/* clear all information for the new selected file */
			lblAccessedDate.Text = null;
			lblCreatedDate.Text = null;
			lblModifiedDate.Text = null;

			OpenFileDialog oFileDialog = new OpenFileDialog();
			
			oFileDialog.InitialDirectory = @"C:\";
			oFileDialog.Filter = "All Files (*.*)|*.*"; 
			oFileDialog.RestoreDirectory = true;

			if(oFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				sPath = oFileDialog.FileName;
			
				statusBar.Text = sPath;

				/* only after the very first file has been choosen */
				Accessed.Enabled = true;
				Modified.Enabled = true;
				FCreated.Enabled = true;
				
				/* return all attributes information */
				GetAllDates();

				if(FCreated.Checked == false)
				{
					FCreated.Checked = true;
				}
				else
				{
					Date.Value = dCreated.Day;
					Month.Value = dCreated.Month;
					Year.Value = dCreated.Year;
				}
			}
		}

		private void FilesManagerUI_Load(object sender, System.EventArgs e)
		{
			FCreated.Checked = true;
			
			if(sPath == "" || sPath == null)
			{
				FCreated.Enabled = false;
				Accessed.Enabled = false;
				Modified.Enabled = false;
			}
		}

		private void Type_CheckedChanged(object sender, System.EventArgs e)
		{
			RadioButton button = (RadioButton)sender;
			
			if(button.Checked)
			{
				switch(button.Name)
				{
					case "FCreated" :
						action = ACTION.CREATED ;
						Date.Value = dCreated.Day;
						Month.Value = dCreated.Month;
						Year.Value = dCreated.Year;
						break;
					case "Modified" :
						action = ACTION.MODIFIED;
						Date.Value = dModified.Day;
						Month.Value = dModified.Month;
						Year.Value = dModified.Year;
						break;
					case "Accessed" :
						action = ACTION.ACCESSED;
						Date.Value = dAccessed.Day;
						Month.Value = dAccessed.Month;
						Year.Value = dAccessed.Year;
						break;
				}
			}
		}

		/* As soon as the month is selected, the max number of days is updated based on the month and year */
		private void Month_ValueChanged(object sender, System.EventArgs e)
		{
			Date.Maximum = DateTime.DaysInMonth(Convert.ToInt32(Year.Value), Convert.ToInt32(Month.Value));
		}

		private void bModify_Click(object sender, System.EventArgs e)
		{
			if(sPath == "" || sPath == null)
			{
				MessageBox.Show("You must choose a file first please.","Failure",MessageBoxButtons.OK, MessageBoxIcon.Information);
				return;
			}
		
			DateTime dNewDate = DateTime.Now;

			switch(action)
			{
				case ACTION.CREATED		:
					dNewDate = new DateTime(Convert.ToInt32(Year.Value),Convert.ToInt32(Month.Value),Convert.ToInt32(Date.Value),dCreated.Hour,dCreated.Minute,dCreated.Second,dCreated.Millisecond);
					break;
				case ACTION.MODIFIED		:
					dNewDate = new DateTime(Convert.ToInt32(Year.Value),Convert.ToInt32(Month.Value),Convert.ToInt32(Date.Value),dModified.Hour,dModified.Minute,dModified.Second,dModified.Millisecond);
					break;
				case ACTION.ACCESSED		:
					dNewDate = new DateTime(Convert.ToInt32(Year.Value),Convert.ToInt32(Month.Value),Convert.ToInt32(Date.Value),dAccessed.Hour,dAccessed.Minute,dAccessed.Second,dAccessed.Millisecond);
					break;
			}
		
			/* we can set any date we want for the creation but we can't 

  • set a date older than the creation date for the other cases */
if(action != ACTION.CREATED) { if(DateTime.Compare(dNewDate, dCreated) < 0) { MessageBox.Show("You can't set this attribute" + ((action == ACTION.ACCESSED)? " Accessed" : " Modified ") + " to an earlier datetime than the Created datetime!", "Failure",MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } else { if((DateTime.Compare(dNewDate, dModified) > 0)||(DateTime.Compare(dNewDate, dAccessed) > 0)) { MessageBox.Show("You can't set this attribute Created " + " to an later datetime than the other datetimes!", "Failure",MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } /* Attributes update */ switch(action) { case ACTION.CREATED : System.IO.File.SetCreationTime(sPath, dNewDate); break; case ACTION.MODIFIED : System.IO.File.SetLastWriteTime(sPath, dNewDate); break; case ACTION.ACCESSED : System.IO.File.SetLastAccessTime(sPath, dNewDate); break; } lblAccessedDate.Text = null; lblCreatedDate.Text = null; lblModifiedDate.Text = null; GetAllDates(); } private void bQuit_Click(object sender, System.EventArgs e) { this.Close(); } private void GetAllDates() { /* string used to display the date */ string sDate = ""; /* return all information about the file attributes */ dAccessed = System.IO.File.GetLastAccessTime(sPath); sDate = dAccessed.ToString(FORMATDATE); lblAccessedDate.Text = lblAccessedDate.Text + sDate; dModified = System.IO.File.GetLastWriteTime(sPath); sDate = dModified.ToString(FORMATDATE); lblModifiedDate.Text = lblModifiedDate.Text + sDate; dCreated = System.IO.File.GetCreationTime(sPath); sDate = dCreated.ToString(FORMATDATE); lblCreatedDate.Text = lblCreatedDate.Text + sDate; } } }

Conclusion :


Je ferais une mise à jour lors de ma prochaine pause entre deux projets au boulot :) c'est pour passer le temps et apprendre des nouvelles choses !

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.