Right click on the project in Solution Explorer and choose Properties > Settings tab Use this page to create individual application settings Scope Typically select 'User'. If you select Application scope then Settings.Default[] will be read-only (save deosn't work) Visual Studio will create the files "Settings.settings" and "Settings.Designer".settings that contain the singleton class Settings. Reading and writing […]
Category: Files
Special Directories
Location To Store Application Data private const string MY_FILE_DIRECTORY_TO_USE Environment::GetFolderPath(Environment::SpecialFolder::ApplicationData) + "\\" + Application::CompanyName + "\\" + Application::ProductName + "\\" Windows 7 path from using the above: C:\Users\MyUserName\AppData\Roaming\Company Name\App Name N.B. SpecialFolder::CommonApplicationData is not the same as ApplicationData. Non-Admin users and applications running as a non admin user do not have permission to write to the CommonApplicationData […]
Write New File
using 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 = new SaveFileDialog(); SelectFileDialog.Filter = "Comma Separated Values (*." + MY_FILE_EXTENSION + ")|*." + MY_FILE_EXTENSION; //"txt files (*.txt)|*.txt|All files (*.*)|*.*" SelectFileDialog.FilterIndex = 1; […]
Open File Dialog
Open File Dialog Example
Working With Files
using System.IO; Does file exist? if (File.Exists("c:\\temp.txt")) Delete File try { if (File.Exists("c:\\temp.txt")) File.Delete("c:\\temp1.txt"); } catch (IOException ) { System.Diagnostics.Debug.WriteLine("This file is in use by another application – please close it first"); } or 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") […]
Working With Directories
using System.IO; Does directory exist? (Note paths can include a filename if you wish) if (!Directory.Exists(Path.GetDirectoryName(MyFileName))) Directory.CreateDirectory(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\\"); […]
Save File As
using System.IO; Save As Dialog Box //—– NEW FILE DIALOG —– const string MY_FILE_EXTENSION = "txt"; String SaveAsFilename; //If last directory is not valid then default to My Documents if (!Directory.Exists(Path.GetDirectoryName(ApMain.LastFileDirectory))) ApMain.LastFileDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); //—– SAVE FILE DIALOG BOX —– SaveFileDialog SelectFileDialog = new SaveFileDialog(); SelectFileDialog.Filter = "Comma Separated Values (*." + MY_FILE_EXTENSION + ")|*." […]