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 […]
Category: File Input and Output
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(); 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 […]
Open File Dialog
using namespace System::IO; Open File Dialog Example String ^LastFileDirectory; String ^Filename; //If last directory is not valid then default to My Documents (if you don’t include this the catch below won’t occur for null strings so the start directory is undefined) if (!Directory::Exists(LastFileDirectory)) LastFileDirectory = Environment::GetFolderPath(Environment::SpecialFolder::MyDocuments); //—– FILE OPEN DIALOG BOX —– OpenFileDialog ^SelectFileDialog = […]
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 […]
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 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) + […]
Storing Class Data, Arrays Etc
See Serialization
Unlocking files used by your application so they can be moved or deleted
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();
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 ^) { } //Display dialog box if (SelectFolderDialog->ShowDialog() == System::Windows::Forms::DialogResult::OK) { SelectedDirectory = SelectFolderDialog->SelectedPath; //…………. }