Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save File to MyDocuments + App Folder

I am trying to save my .NET application settings file to the user's %MyDocument%\MyApplication folder, but I don't know how to check for an existing folder\file, and create or append the folder\file on save. I don't want to open a saveFileDialog because I need the file to be in the same place on all user computers. This is what I have so far, but it isn't working. 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
like image 679
Jeagr Avatar asked Feb 14 '13 00:02

Jeagr


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
}
like image 109
CC Inc Avatar answered Sep 27 '22 18:09

CC Inc


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

like image 23
user2378769 Avatar answered Sep 27 '22 18:09

user2378769