FileAttributes (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This examples demonstrates the use of getting and setting the attributes to a file.

Code

void __fastcall TForm1::btnBrowseClick(TObject *Sender)
{
	TFileAttributes LFileAttributes;
	try
	{
		/* Browse a file and read its attributes */
		if (OpenDialog1->Execute())
			LFileAttributes = TFile::GetAttributes(OpenDialog1->FileName);

		cbReadOnly->Checked = LFileAttributes.Contains(TFileAttribute::faReadOnly);
		cbHidden->Checked = LFileAttributes.Contains(TFileAttribute::faHidden);
		cbSystem->Checked = LFileAttributes.Contains(TFileAttribute::faSystem);
		cbDirectory->Checked = LFileAttributes.Contains(TFileAttribute::faDirectory);
		cbArchive->Checked = LFileAttributes.Contains(TFileAttribute::faArchive);
		cbDevice->Checked = LFileAttributes.Contains(TFileAttribute::faDevice);
		cbNormal->Checked = LFileAttributes.Contains(TFileAttribute::faNormal);
		cbTemporary->Checked = LFileAttributes.Contains(TFileAttribute::faTemporary);
		cbSparseFile->Checked = LFileAttributes.Contains(TFileAttribute::faSparseFile);
		cbReparsePoint->Checked = LFileAttributes.Contains(TFileAttribute::faReparsePoint);
		cbCompressed->Checked = LFileAttributes.Contains(TFileAttribute::faCompressed);
		cbOffline->Checked = LFileAttributes.Contains(TFileAttribute::faOffline);
		cbNotContentIndexed->Checked = LFileAttributes.Contains(TFileAttribute::faNotContentIndexed);
		cbEncrypted->Checked = LFileAttributes.Contains(TFileAttribute::faEncrypted);
	}
	catch (...)
	{
		/* Catch the possible exceptions */
		MessageDlg("No file selected", mtError, TMsgDlgButtons() << mbOK, 0);
		return;
	}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnSaveChangesClick(TObject *Sender)
{
	TFileAttributes LFileAttributes;
	try
	{
		/* Check for attributes */
		if (cbReadOnly->Checked)
			LFileAttributes = LFileAttributes << TFileAttribute::faReadOnly;

		if (cbHidden->Checked)
			LFileAttributes = LFileAttributes << TFileAttribute::faHidden;

		if (cbSystem->Checked)
			LFileAttributes = LFileAttributes << TFileAttribute::faSystem;

		if (cbArchive->Checked)
			LFileAttributes = LFileAttributes << TFileAttribute::faArchive;

		if (cbCompressed->Checked)
			LFileAttributes = LFileAttributes << TFileAttribute::faCompressed;

		if (cbOffline->Checked)
			LFileAttributes = LFileAttributes << TFileAttribute::faOffline;

		/* Save the attributes to the opened file */
		TFile::SetAttributes(OpenDialog1->FileName, LFileAttributes);
	}
	catch (...)
	{
		/* Catch the possible exceptions */
		MessageDlg("No file selected", mtError, TMsgDlgButtons() << mbOK, 0);
		return;
	}
}
//---------------------------------------------------------------------------

Uses