File Access General

There are 2 classes, depending on whether you are doing a single file operation, or whether you need repeated cached file access over a length of time: (more…)

Filename Not Permitted Characters


//Remove any characters not allowed in filenames characters:-  \ / | : * " ? < >
	MyString = MyString->Replace("\\", "-");
	MyString = MyString->Replace("/", "-");
	MyString = MyString->Replace("|", "-");
	MyString = MyString->Replace(":", "-");
	MyString = MyString->Replace("*", "-");
	MyString = MyString->Replace("\"", " ");
	MyString = MyString->Replace("?", " ");
	MyString = MyString->Replace("<", " ");
 	MyString = MyString->Replace(">", " ");

Obtaining Hard Disk Information


using namespace System::IO;

	try
	{
		array<DriveInfo^> ^allDrives = DriveInfo::GetDrives();

 (more...)

Open File Dialog


using namespace System::IO;

Open File Dialog Example


	String ^LastFileDirectory;
	String ^Filename;

 (more...)

Read File


using namespace System::IO;

Read Bytes From A File


	__int64 FileByteNumber;
	int ReadByte;

 (more...)

Reading File Version


	try
	{
		FileVersionInfo ^FileVersion1 = FileVersionInfo::GetVersionInfo(Environment::GetFolderPath(Environment::SpecialFolder::ProgramFiles) + "\\Some Folder\\SomeApplication.exe");
		sTemp = "App Version: " + Convert::ToString(FileVersion1->FileVersion) + "\r\n";
	}
	catch (Exception ^)
	{
	}

Special Directories


using namespace System::Windows::Forms;
using namespace System::IO;

Exe path Directory

(more…)

Storing Class Data, Arrays Etc

See Serialization

User select directory example


	String ^SelectedDirectory;

	FolderBrowserDialog ^SelectFolderDialog = gcnew FolderBrowserDialog();

	//Setup dialog box
	SelectFolderDialog->Description = "Select directory to store files to";
	SelectFolderDialog->ShowNewFolderButton = true;
	SelectFolderDialog->RootFolder = System::Environment::SpecialFolder::Desktop;
	try
	{
		SelectFolderDialog->SelectedPath = SelectedDirectory;		//Try and use last selection
	}
	catch (Exception ^)
	{
	}

 (more...)

Working With Directories


using namespace System::IO;

Does directory exist?

(Note paths can include a filename if you wish) (more…)

Working With Files


using namespace System::IO;

Does file exist?


	if (File::Exists("c:\\temp.txt"))

(more…)

Write File Thread Safe

This example is based on writing a file from asynchronously received TCP packets.  FileStream->Write is not thread safe, so this approach deals with letting file write occur in the background and waiting for it to complete before writing the next received block of data bytes. (more…)

Write New File


using namespace System::IO;

Save As Dialog Box


	String ^SaveAsFilename;

 (more...)