Reputation: 33
I have developed a Windows Form Program in C# with Visual Stuido 2013. When it is installed, I would like the exe.config file to be in the AppData path. Becuase sometimes the program will have to read and write that file and I don't want the program has to be executed with administrator rights.
When I create the installer with a setup project, I add to the Application Folder the "Primary output from the program", but I can not specify that the exe.config may place into the AppData path.
Is there some way to do that in order to avoid the administrator rights?
Thank you.
Upvotes: 0
Views: 1782
Reputation: 66
using System.Configuration; using System.IO;
Configuration config = null;
public Program()
{
string app_data = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
StreamWriter sw = new StreamWriter(app_data + "\\MyApp.exe.config", false);
sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
sw.WriteLine("<configuration>");
sw.WriteLine("</configuration>");
sw.Close();
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = app_data + "\\MyApp.exe.config";
config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
config.AppSettings.Settings.Add("Key", "Value");
config.Save();
Console.ReadLine();
}
Upvotes: 3
Reputation: 66
Why not just install the application in AppData?
string[] files = Directory.GetFiles(".\\");
string app_data = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Apps\\MyApp";
Directory.CreateDirectory(app_data);
foreach (var file in files)
{
File.Copy(file, app_data + file.Substring(file.LastIndexOf("\\")) );
}
Upvotes: 1