编写文件配置时出现问题 [英] Problem writing the file config

查看:121
本文介绍了编写文件配置时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的VB.net程序中,不是C或C#我读了我的Config文件,例如:



In my program in VB.net, NOT C or C # I read my Config file for example like this:

Imports System.Configuration
Imports System.Collections.Specialized
Imports System.Collections.Generic

Module Module1

    Sub LeggiConfig()

        Dim sAttr As String

        sAttr = ConfigurationSettings.AppSettings("Key1") ' li legge ad uno ad uno
        Console.WriteLine("The value of Key1: " & sAttr)
        
        end sub
   End Module







<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>

  <appSettings>
    
    <add key="Key0" value="0" />
    <add key="Key1" value="1" />
    <add key="Key2" value="2" />
  </appSettings>
  
</configuration>





我想更改Key1参数,我尝试了以下方法:





I would like to change that Key1 parameter and I tried these ways:

ConfigurationSettings.AppSettings.Set(("Key1"), "pippo" & " ")
My.Settings.Save()
 sAttr = ConfigurationSettings.AppSettings("Key1") ' li legge ad uno ad uno
       Console.WriteLine("The value of Key1: " & sAttr)





如果我在控制台中这样写,我看到值pippo,但在配置文件中它仍然是初始值1



如果相反我添加这一行:





if I write so in the console I see the value pippo, but in the config file it still remains the initial value that was 1

if instead I add this line:

' ConfigurationSettings.AppSettings.Add(("Key1"), "pippo" & " ")





我有这个错误:



System.Configuration.ConfigurationErrorsException:'只读配置。'



这是如何解决的?



我尝试过的事情:



你能帮忙吗?我?



因为我试过我在上面写的



I have this error:

System.Configuration.ConfigurationErrorsException: 'Read-only configuration.'

how does this resolve?

What I have tried:

can you help Me ?

as I tried I wrote it above

推荐答案

.config文件通常位于与Program Files相同的文件夹。 Program Files下的所有内容都是ReadOnly,包括你的.config文件,给普通用户。只要代码以提升的权限运行(以管理员身份运行),机器上的管理员就没有这个限制。



这是一个不好的IDEA在Program Files下打开权限,以便用户可以写入配置文件。您可以在其自己的配置文件中将您的附加所有用户在计算机上设置放在C:\ ProgramData下的文件夹中和/或用户配置文件文件夹中自己的配置文件中的用户特定设置中。确切哪些文件夹是合适的取决于用户应该使用该应用程序的情况。



查看 Environment.SpecialFolder Enum(System)| Microsoft Docs [ ^ ]您可以获取路径的文件夹位置以及它们通常用于的路径,并且 Environment.GetFolderPath Method(System)| Microsoft Docs [ ^ ]如何将这些值解析为文件夹路径。



顺便说一下,ConfigurationSettings现已过时,已被替换为 ConfigurationManager [ ^ ]。
The .config file is usually sitting in the same folder as the application, under Program Files. Everything under Program Files is ReadOnly, including your .config file, to normal users. Administrators on the machine don't have that restriction, so long as the code is running with the elevated permissions ("run as Administrator").

It is a BAD IDEA to open up permissions under Program Files so users can write to the config file. You can put your additional "all users on the machine" settings for you app in it's own configuration file in a folder under C:\ProgramData and/or "user-specific" settings in it's own configuration file in the users profile folder. Exactly which folders are appropriate depend on the situation the users are expected to use the app.

Check out Environment.SpecialFolder Enum (System) | Microsoft Docs[^] for the folder locations you can get the paths for and what they are normally used for, and Environment.GetFolderPath Method (System) | Microsoft Docs[^] for how to resolve those values to folder paths.

By the way, ConfigurationSettings is now obsolete having been replaced by ConfigurationManager[^].


首先查看实际的配置文件及其属性。该文件是否标记为只读?

检查配置文件所在的文件夹:谁拥有该文件夹的写入权限?您的应用程序在哪个ID下运行?



如果无法解决问题,请查看此处: Steve Michelotti:ConfigurationErrorsException - 配置是只读的 [ ^ ] - 讨厌说它但是它在C#中,但这可能会有所帮助:代码转换器C#到VB和VB到C# - Telerik [ ^ ]
Start by looking at the actual configuration file and its properties. Is the file marked as read only?
Check the folder the config file is in: who has write permission to the folder? What ID is your app running under?

If that doesn't fix it, look here: Steve Michelotti: ConfigurationErrorsException - The configuration is read only[^] - hate to say it but it's in C#, but this may help: Code Converter C# to VB and VB to C# – Telerik[^]


即使您有权编辑配置文件,也不能简单地写入 AppSettings 集合。您必须打开EXE配置,更改该实例上的设置,保存该实例,然后刷新主 ConfigurationManager 设置:

Even if you have permission to edit the config file, you can't simply write to the AppSettings collection. You have to open the EXE configuration, change the settings on that instance, save that instance, and then refresh the main ConfigurationManager settings:
Configuration configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
KeyValueConfigurationCollection settings = configFile.AppSettings.Settings;

if (settings["Key1"] == null)
{
    settings.Add("Key1", "pippo");
}
else
{
    settings["Key1"].Value = "pippo";
}

configFile.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);



c# - ConfigurationManager.AppSettings - 如何修改和保存? - 堆栈溢出 [ ^ ]


这篇关于编写文件配置时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆