Managing Files in C# - Renaming and File Attributes (Page 4 of 5 )
It is pretty much self-explanatory that to rename a file you need the source file as well as its new name. We will accomplish the renaming action using the same approach we used while removing a file. The only difference is that we don't change the file's path, just its name. But this can also be done using System.IO.File.Move().
First, as soon as the user clicks on btnRename, we display the dialog box. Then the user picks the source file and we resize frmMain, revealing lblNewFileName, txtNewFileName, and two buttons (btnGoRename and btnCancel). The txtNewFileName field is initialized with the following text value: "type here the new name with extension."
private void btnRename_Click(object sender, EventArgs e)
{
sourceFileName = null;
openFileDialog.ShowDialog();
if (!System.IO.File.Exists(sourceFileName))
{
MessageBox.Show("The selected file either does not exist or you didn't pick any.");
return;
}
this.ClientSize = new System.Drawing.Size(415, 158);
this.lblNewFileName.Show();
this.txtNewFileName.Show();
this.btnGoRename.Show();
this.btnCancel.Show();
}
The user is required to specify the new name of the source file in the text field called txtNewFileName. We do a quick bit of error handling to eliminate the possibility of it being the said "type here the new name with extension" or null. Then we move on to actually rename the file (using remove, but not changing the destination path, just the name of the file) and then resizing the frmMain to conceal components we don't need.
private void btnGoRename_Click(object sender, EventArgs e)
{
destFileName = txtNewFileName.Text;
if (destFileName == "type here the new name with extension" || destFileName == "" || destFileName == null)
{
MessageBox.Show("You didn't pick the new file name.");
return;
}
System.IO.File.Move(sourceFileName, destFileName);
MessageBox.Show("Done with success.");
this.lblNewFileName.Hide();
this.txtNewFileName.Hide();
this.btnGoRename.Hide();
this.btnCancel.Hide();
this.ClientSize = new System.Drawing.Size(145, 158);
}
We can't forget about the btnCancel either. If the user clicked on btnRename from the main menu and the rename components are displayed, the form is resized, and all that happens; but what if s/he wants to go back to the main menu and choose Copy instead of Rename? Our btnCancel conceals every rename component, resizes frmMain to its original size, and basically everything goes back to the menu.
private void btnCancel_Click(object sender, EventArgs e)
{
this.lblNewFileName.Hide();
this.txtNewFileName.Hide();
this.btnGoRename.Hide();
this.btnCancel.Hide();
this.ClientSize = new System.Drawing.Size(145, 158);
}
There's one more thing left for us to code and implement, and that's the btnProperties. When the user clicks on this button, the dialog box is displayed and the user picks the source file. Error handling isn't forgotten. We resize frmMain and display the large txtProperties text box (that's where the attributes of the file will be reported) and the btnHidden button (this goes back to the menu resizing the form).
private void btnProperties_Click(object sender, EventArgs e)
{
this.txtProperties.Text = null;
sourceFileName = null;
openFileDialog.ShowDialog();
if (!System.IO.File.Exists(sourceFileName))
{
MessageBox.Show("The selected file either does not exist or you didn't pick any.");
return;
}
this.ClientSize = new System.Drawing.Size(415, 158);
this.txtProperties.Show();
this.btnHide.Show();
StringBuilder stbOutput = new StringBuilder("");
System.IO.FileAttributes Attributes = System.IO.File.GetAttributes(sourceFileName);
stbOutput.Append("Date of Creation: " + System.IO.File.GetCreationTime(sourceFileName) + "rn");
stbOutput.Append("Date of Modification: " + System.IO.File.GetLastWriteTime(sourceFileName) + "rn");
stbOutput.Append("Date of Last Access: " + System.IO.File.GetLastAccessTime(sourceFileName) + "rn");
stbOutput.Append("Attributes: rn");
stbOutput.Append("Archive: " + Convert.ToBoolean((Attributes & System.IO.FileAttributes.Archive) == (System.IO.FileAttributes.Archive)) + " ");
stbOutput.Append("Directory: " + Convert.ToBoolean((Attributes & System.IO.FileAttributes.Directory) == (System.IO.FileAttributes.Directory)) + "rn");
stbOutput.Append("Encrypted: " + Convert.ToBoolean((Attributes & System.IO.FileAttributes.Encrypted) == (System.IO.FileAttributes.Encrypted)) + " ");
stbOutput.Append("Hidden: " + Convert.ToBoolean((Attributes & System.IO.FileAttributes.Hidden) == (System.IO.FileAttributes.Hidden)) + "rn");
stbOutput.Append("Normal: " + Convert.ToBoolean((Attributes & System.IO.FileAttributes.Normal) == (System.IO.FileAttributes.Normal)) + " ");
stbOutput.Append("Read-only: " + Convert.ToBoolean((Attributes & System.IO.FileAttributes.ReadOnly) == (System.IO.FileAttributes.ReadOnly)) +"rn");
stbOutput.Append("System: " + Convert.ToBoolean((Attributes & System.IO.FileAttributes.System) == (System.IO.FileAttributes.System)) + " ");
stbOutput.Append("Temporary: " + Convert.ToBoolean((Attributes & System.IO.FileAttributes.Temporary) == (System.IO.FileAttributes.Temporary)));
txtProperties.Text = stbOutput.ToString();
}
The above code snippet is more long than complex. It surely isn't as hard as it seems. After the source file is chosen, we build a string called stbOutput. We build this with the data that's going to be displayed. Working this way is easy because we just use the Append method. We will use the System.IO.File.GetAttributes() to "grab" the attributes of the source file in a structure with System.IO.FileAttributes type.
Now that we have our structure, we just need to examine its content. We go through the structure using the System.IO.FileAttributes.<attribute goes here> as a basis for our comparison and if they match, then we simply append a "TRUE" in our string or "FALSE" if the particular attribute isn't set for the source file. Once we've analyzed enough attributes, then we actually display our string on the txtProperties text box.
Finally, as I mentioned earlier, we need the btnHide button to conceal the unnecessary components if the user opts to go back to the main menu. S/he might have clicked on Properties, but then decided that Renaming the file would be a better option. Clicking on btnHide resets txtProperties, conceals both itself (btnHide) and the txtProperties, and ultimately resizes frmMain to its original size.
private void btnHide_Click(object sender, EventArgs e)
{
this.txtProperties.Text = "rn";
this.btnHide.Hide();
this.txtProperties.Hide();
this.ClientSize = new System.Drawing.Size(145, 158);
}
And with this we can finally finish our project. If you could follow along and have done everything as it was stated, then your application should run without problems. Regardless, if yours doesn't work, on the next page you can download the archived project and see what you did wrong. You can play around with it.
Next: Final Words >>
More C# Articles
More By Barzan "Tony" Antal