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\\"); […]
Category: File Input and Output
Working With Files
using namespace System::IO; Does file exist? if (File::Exists(“c:\\temp.txt”)) Copy File File::Copy(“c:\\temp1.txt”, “c:\\temp2.txt”); Move File File::Move(“c:\\dir1\\temp1.txt”, “c:\\dir2\\temp1.txt”); //Source, Destination Delete File try { if (File::Exists(“c:\\temp.txt”)) { //If file has read only attribute set clear it so that delete can occur if ((File::GetAttributes(“c:\\temp1.txt”) & FileAttributes::ReadOnly) == FileAttributes::ReadOnly) File::SetAttributes(“c:\\temp1.txt”, FileAttributes::Normal); File::Delete(“c:\\temp1.txt”); } } catch (IOException ^) { MessageBox::Show(L”This […]
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. In your header file FileStream ^ClientRxFileStream1; IAsyncResult ^ClientRxFileStreamAsyncResult1; array<Byte> ^ClientReceiveFileWriteBytes; […]
Write New File
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 = […]