Sometimes it is necessary to force the background garbage collector to finish up all its tasks so a file recently used can be moved or deleted: //First force the garbage collector to finish up so the file is released from its last access GC::Collect(); GC::WaitForPendingFinalizers();
Category: File Input and Output
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; […]
Storing Class Data, Arrays Etc
See Serialization
Obtaining Hard Disk Information
using namespace System::IO; try { array<DriveInfo^> ^allDrives = DriveInfo::GetDrives(); for each (DriveInfo ^drive in allDrives) { sTemp += “Drive Name: ” + drive->Name; sTemp += “, Type ” + Convert::ToString(drive->DriveType); if ((drive->DriveType == DriveType::Fixed) && (drive->IsReady)) { sTemp += “, Volume Label: ” + drive->VolumeLabel; sTemp += “, File System: ” + drive->DriveFormat; if (drive->TotalSize […]
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 ^) { }
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: File – Static methods for one off access FileInfo – Create an instance of a FileInfo class to access files over a length of time. Both have the […]
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(“>”, […]
Special Directories
using namespace System::Windows::Forms; using namespace System::IO; Exe path Directory Reflection::Assembly::GetExecutingAssembly()->Location Application Directory Application path Application::ExecutablePath //This gives you the full path including the filename Path::GetDirectoryName(Application::ExecutablePath) //This gives you the path to the directory (without trailing '\') or use String ^sPath sPath = Application::ExecutablePath; sPath = Path::GetDirectoryName(sPath); Location To Store Application Data #define MY_FILE_DIRECTORY_TO_USE Environment::GetFolderPath(Environment::SpecialFolder::ApplicationData) + […]
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 = […]
Read File
using namespace System::IO; Read Bytes From A File __int64 FileByteNumber; int ReadByte; FileStream ^SourceFile = gcnew FileStream(SelectFileDialog->FileName, FileMode::Open, FileAccess::Read, FileShare::None); for (FileByteNumber = 0; FileByteNumber < SourceFile->Length; FileByteNumber++) { //—– READ EACH BYTE —– if ((ReadByte = SourceFile->ReadByte()) == -1) throw gcnew Exception("File read too short"); //ReadByte has the value but is actually an integer […]