using namespace System::IO;

Does directory exist?

(Note paths can include a filename if you wish)


	if (!Directory::Exists(Path::GetDirectoryName(MyFileName)))
	//or
	String ^path;
	path = "C:\\Atest\\";				//		'\\'='\'
	if (!Directory::Exists(path))
	//or
	if (!Directory::Exists("C:\\Atest\\"))	//		'\\'='\'

Create Directory

(Note paths can include a filename if you wish)


	Directory::CreateDirectory(Path::GetDirectoryName(MyFileName));
	//or
	String ^path;
	path = "C:\\Atest\\";				//		'\\'='\'
	Directory::CreateDirectory(path);
	//or
	Directory::CreateDirectory("C:\\Atest\\");	//		'\\'='\'

Delete Directory


	//----- DELETE FOLDER IF IT ALREADY EXISTS -----
	if (Directory::Exists("C:\\myfolder\\"))
	{
		try
		{
			array<String^> ^files = Directory::GetFiles("C:\\myfolder\\");
			for each (String ^sTemp in files)
			{
				try
				{
					if (File::Exists(sTemp))
					{
						//If file has read only attribute set clear it so that delete can occur
						if ((File::GetAttributes(sTemp) & FileAttributes::ReadOnly) == FileAttributes::ReadOnly)
							File::SetAttributes(sTemp, FileAttributes::Normal);

						File::Delete(sTemp);
					}
				}
				catch (IOException ^)
				{
					//Can't delete file
				}
			}
			Directory::Delete("C:\\myfolder\\", true);	//including true will cause subdirectories to be deleted

		}
		catch (IOException ^)
		{
			//Can't delete folder
		}
	}

Move Up 1 Subdirectory (when a directory no longer exists)


	//If last directory is not valid then try directory before it
	if (!Directory::Exists(Path::GetDirectoryName(LastFileDirectory)))
	{
		if (LastFileDirectory->LastIndexOf("\\") >= 0)
			LastFileDirectory = LastFileDirectory->Substring(0, (LastFileDirectory->LastIndexOf("\\")));		//Delete trailing '\'
		if (LastFileDirectory->LastIndexOf("\\") >= 0)
			LastFileDirectory = LastFileDirectory->Substring(0, (LastFileDirectory->LastIndexOf("\\") + 1));	//Delete directory name up to next '\'
	}

Get Directory Attributes


	 txtOutput->Text = "Name: " + dir->FullName + "\r\n";
	 txtOutput->Text += "Created: " + dir->CreationTime.ToShortDateString() +  " " + dir->CreationTime.ToLongTimeString();

Get List Of Files In A Directory


	String ^sTemp;
	array<String^> ^files = Directory::GetFiles(SelectedDirectory);
	for each(sTemp in files)
	{
		sTemp = sTemp->Substring(SelectedDirectory->Length + 1);
		txtFilesToConvert->Text += sTemp + "\r\n";
	}

Get List Of SubDirectories In A Directory


	String ^sTemp;
	array<string^> ^subDirs = Directory::GetDirectories(SelectedDirectory);
	for each(sTemp in subDirs)
	{
		sTemp = sTemp->Substring(SelectedDirectory->Length + 1);
		txtFilesToConvert->Text += sTemp + "\r\n";
	}

Delete Old Files In A Directory


	DateTime DeleteBeforeDateTime = DateTime::Now - TimeSpan(2, 0, 0, 0);		//Delete files > 2 days old
	array<String^> ^files = Directory::GetFiles("C:\\MyFolder\\");
	for each(sTemp in files)
	{
		if (File::GetCreationTime(sTemp) < DeleteBeforeDateTime)
		{
			try
			{
				if (File::Exists(sTemp))
				{
					//If file has read only attribute set clear it so that delete can occur
					if ((File::GetAttributes(sTemp) & FileAttributes::ReadOnly) == FileAttributes::ReadOnly)
						File::SetAttributes(sTemp, FileAttributes::Normal);

					File::Delete(sTemp);
				}
			}
			catch (IOException ^)
			{
				GlobalObjects::EventLog1->AddEvent(true, 0x00, "ExportVideoServer::ProcessTcpServerEvent can't delete old file: " + sTemp);
			}
		}
	}
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.