using namespace System::IO;

Save As Dialog Box


	String ^SaveAsFilename;

	//If last directory is not valid then default to My Documents
	if (!Directory::Exists(Path::GetDirectoryName(LastFileDirectory)))
		LastFileDirectory = Environment::GetFolderPath(Environment::SpecialFolder::MyDocuments);

	//----- SAVE FILE DIALOG BOX -----
	SaveFileDialog ^SelectFileDialog = gcnew SaveFileDialog();

	SelectFileDialog->Filter = "Comma Separated Values (*." + MY_FILE_EXTENSION + ")|*." + MY_FILE_EXTENSION;		//"txt files (*.txt)|*.txt|All files (*.*)|*.*"
	SelectFileDialog->FilterIndex = 1;				//(First = 1, not 0)
	SelectFileDialog->Title = "Save Live Log As";
	SelectFileDialog->OverwritePrompt = true;
	try
	{
		SelectFileDialog->InitialDirectory = LastFileDirectory;
	}
	catch (Exception ^)
	{
		SelectFileDialog->InitialDirectory = Environment::GetFolderPath(Environment::SpecialFolder::MyDocuments);
	}
	//Display dialog box
	if (SelectFileDialog->ShowDialog() == System::Windows::Forms::DialogResult::OK)		//This ensures correct filename extension is used
	{
		//----- SAVE THE FILE -----
		SaveAsFilename = SelectFileDialog->FileName;

		//STORE LAST USED DIRECTORY
		if (SaveAsFilename->LastIndexOf("\\") >= 0)
			LastFileDirectory = SaveAsFilename->Substring(0, (SaveAsFilename->LastIndexOf("\\") + 1));
		else
			LastFileDirectory = Environment::GetFolderPath( Environment::SpecialFolder::MyDocuments);

Write StringTo A New File


	StreamWriter ^StreamWriter1= gcnew StreamWriter("C:\\test.txt");
	//StreamWriter ^StreamWriter1= gcnew StreamWriter("C:\\test.txt", false, System::Text::Encoding::Unicode);	//False = overwrite - Alternative method specifying the encoding
	StreamWriter1->Write(MyString);
	StreamWriter1->Close();

Write Bytes To A New File

(Write unsigned char or arrays of unsigned char to a file – not strings or anything else)


	FileStream ^HeaderOutputFile = gcnew FileStream(MyFilePath, FileMode::Create,
								FileAccess::Write, FileShare::None);

	HeaderOutputFile->WriteByte(0x01);
	//...
	HeaderOutputFile->Close();

Write Binary (Inc Strings) To A New File

The problem with writing using FileStream is that its not string friendly. You can write strings using the StreamWriter, but if you want to write strings and binary data then it's easier to use the Binary Writer.


	String ^sTemp;

	FileStream ^MyOutputFile = gcnew FileStream("C:\\test.txt", FileMode::Create, FileAccess::Write, FileShare::None);
	BinaryWriter ^MyOutputWriter = gcnew BinaryWriter(MyOutputFile);

	sTemp = "Hello World!\r\n";
	MyOutputWriter->Write(sTemp->ToCharArray());	//ToCharArray to avoid String type byte being written at start of the line
	MyOutputWriter->Write(Convert::ToByte(0x01));	//Write will write any variable it is given using as many bytes as needed
	//...
	MyOutputWriter->Close();
	MyOutputFile->Close();

	//GOOD IDEA TO ADD THIS TO TRY CATCH BLOCK AND REMOVE THE TWO ->CLOSE() LINES ABOVE:-
	finally
	{
		if (MyOutputWriter != nullptr)
			MyOutputWriter->Close();
		if (MyOutputFile != nullptr)
			MyOutputFile->Close();
	}
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.