IOUtilsFileAttr (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

The following example uses an edit control, four check boxes, and three buttons. The example describes how to read attributes from a file and how to set them. When you click the Get button, the code reads a set of attributes from a file entered in the edit control and engages one or more of the four check boxes. When you select the desired attributes and press the Set button, the code applies the attributes to a file declared in the edit control. This example code does a file existence check first.

Code

void __fastcall TIOMain::btSetAttrClick(TObject *Sender)
{
	attrs.Clear();

	/* This part of code creates the set of attributes based on
	   the values of the check boxes. */

	if (CheckBox1->Checked)
		attrs = attrs << TFileAttribute::faNormal;

	if (CheckBox2->Checked)
		attrs = attrs << TFileAttribute::faArchive;

	if (CheckBox3->Checked)
		attrs = attrs << TFileAttribute::faSystem;

	if (CheckBox4->Checked)
		attrs = attrs << TFileAttribute::faHidden;

	/* This portion verifies that the filename entered in the edit
	   control exists and applies the set of attributes. */
	if (TFile::Exists(Edit1->Text))
		TFile::SetAttributes(Edit1->Text, attrs);
}

void __fastcall TIOMain::btGetAttrClick(TObject *Sender)
{
	/* If the filename declared in the edit control exists, read
	   it from the file and display the results on one or more
	   of the four check boxes. */

	if (TFile::Exists(Edit1->Text))
	{
		attrs = TFile::GetAttributes(Edit1->Text);

		if (attrs.Contains(TFileAttribute::faNormal))
			CheckBox1->Checked = true;

		if (attrs.Contains(TFileAttribute::faArchive))
			CheckBox2->Checked = true;

		if (attrs.Contains(TFileAttribute::faSystem))
			CheckBox3->Checked = true;

		if (attrs.Contains(TFileAttribute::faHidden))
			CheckBox4->Checked = true;
	}
}

Uses