如何将exe.config文件放在安装的AppData中?
How to place the exe.config file in the AppData in the installation?
我使用 Visual Stuido 2013 在 C# 中开发了一个 Windows 表单程序。安装时,我希望 exe.config 文件位于 AppData 路径中。因为有时程序必须读取和写入该文件,而我不希望程序必须以管理员权限执行。
当我使用安装项目创建安装程序时,我将 "Primary output from the program" 添加到应用程序文件夹,但我无法指定 exe.config 可以放入 AppData 路径。
有什么方法可以避免管理员权限吗?
谢谢。
为什么不直接在 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("\")) );
}
使用System.Configuration;
使用 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();
}
我使用 Visual Stuido 2013 在 C# 中开发了一个 Windows 表单程序。安装时,我希望 exe.config 文件位于 AppData 路径中。因为有时程序必须读取和写入该文件,而我不希望程序必须以管理员权限执行。
当我使用安装项目创建安装程序时,我将 "Primary output from the program" 添加到应用程序文件夹,但我无法指定 exe.config 可以放入 AppData 路径。
有什么方法可以避免管理员权限吗?
谢谢。
为什么不直接在 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("\")) );
}
使用System.Configuration; 使用 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();
}