Save the file to the MyDocuments + App folder

I'm trying to save the .NET application settings file in the user folder MyDocument% \ MyApplication, but I don’t know how to check the existing folder \ file, and also create or add the folder \ file when saving, I don’t want to open saveFileDialog, because I need to so that the file is in one place on all user computers. This is what I have so far, but it does not work. Any help would be appreciated:

var saveSettings = settingsList.text; //assign settings to a variable saveSettings = Regex.Replace(saveSettings, @"\s+", "").Trim() + Environment.NewLine; //remove any extra spaces and add a carriage return so that each setting is on a new line var fileName = string.Format("{0}\\{1}", Environment.SpecialFolder.MyDocuments + "\\MyApp\\", "settings.dat"); //generate path to settings.dat File.AppendAllText(fileName, saveSettings); //save settings.dat 
+7
source share
2 answers
 string path = System.IO.Path.Combine(Environment.GetFolderPath( Environment.SpecialFolder.MyDoc‌​uments),"MyApp","settings.dat"); if(Directory.Exists(path)) { //Exists } else { //Needs to be created } 
+27
source

System.IO.Directory.CreateDirectory (Server.MapPath (path)); // you do not need to check if it exists first

+3
source

All Articles