在运行时使用C#授予写访问权限

在运行时使用C#授予写访问权限,c#,C#,在运行期间,我需要更改app.config文件。如何使用c#进行更改?您需要在项目中添加对System.Configuration的引用。然后,您可以使用以下代码修改可执行文件的app.config: // Open App.Config of executable System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

在运行期间,我需要更改app.config文件。如何使用c#

进行更改?您需要在项目中添加对
System.Configuration
的引用。然后,您可以使用以下代码修改可执行文件的app.config:

// Open App.Config of executable
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Add an Application Setting.
config.AppSettings.Settings.Remove("LastDateFeesChecked");
config.AppSettings.Settings.Add("LastDateFeesChecked", DateTime.Now.ToShortDateString());
// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");
注意:此代码在调试时似乎不起作用。您必须在“发布模式”下运行代码才能使其工作



这是一个关于代码的项目。

您需要在项目中添加对
系统配置的引用。然后,您可以使用以下代码修改可执行文件的app.config:

// Open App.Config of executable
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Add an Application Setting.
config.AppSettings.Settings.Remove("LastDateFeesChecked");
config.AppSettings.Settings.Add("LastDateFeesChecked", DateTime.Now.ToShortDateString());
// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");
注意:此代码在调试时似乎不起作用。您必须在“发布模式”下运行代码才能使其工作


这里有一个关于代码的项目。

不确定,但试试这个

使用名称空间设置引用

using System.Configuration;
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection configSection = config.AppSettings;

try {
  if (configSection != null) {
    if (configSection.IsReadOnly() == false && configSection.SectionInformation.IsLocked == false) {
      configSection.Settings("KeyName").Value = "NewValue";
    config.Save();
    }
  }
}   
catch (ConfigurationException ex) {
   MessageBox.Show(ex.Message, "Configuration Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
不确定,但试试这个

使用名称空间设置引用

using System.Configuration;
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection configSection = config.AppSettings;

try {
  if (configSection != null) {
    if (configSection.IsReadOnly() == false && configSection.SectionInformation.IsLocked == false) {
      configSection.Settings("KeyName").Value = "NewValue";
    config.Save();
    }
  }
}   
catch (ConfigurationException ex) {
   MessageBox.Show(ex.Message, "Configuration Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

啊。。你没有。你的问题可以解释为“我没有权利写它”。这是正常的。程序或普通用户不能编辑应用程序文件夹。将非静态配置存储在其他地方(CommonAppData特殊文件夹)。

AH。。你没有。你的问题可以解释为“我没有权利写它”。这是正常的。程序或普通用户不能编辑应用程序文件夹。将非静态配置存储在其他位置(CommonAppData特殊文件夹)。

是否要使用相同的应用程序app.config?是否要使用相同的应用程序app.config??