简体   繁体   English

如何写入非标准配置文件?

[英]How do I write to a non-standard config file?

I have a file called middle.config that is deployed in the same directory as an exe, but I need to update values in this file. 我有一个名为middle.config的文件,该文件与exe部署在同一目录中,但是我需要更新此文件中的值。 That means that I have to go to C:\\Program Files (x86)\\ directory to access the file. 这意味着我必须转到C:\\ Program Files(x86)\\目录才能访问该文件。 Although it is named as a .config file it does not follow the usual schema of a .config file. 尽管它被命名为.config文件,但它并不遵循.config文件的常规架构。 It looks like this: 看起来像这样:

<configuationSettings>     
  <middleSettings      
   groupName="XYZ"     
   forkName="SomeDbName"     
   dbServerName="123.123.123.123"     
   cnnTimeoutSeconds="30"     
   cmdDefaultTimeoutSeconds="30"     
   cmdMediumTimeoutSeconds="60"     
   cmdLongTimeoutSeconds="480"     
   />     
 <userKeys>     
   <Assemblies value="C:\assemblies\" />     
 </userKeys>     
 <friendlyDbName value="NiceData"/>     
</configuationSettings>     

I'm able to read and manipulate the content with Xml, but when I try to save the file back, a "No Permissions" error is thrown. 我可以使用Xml读取和操作内容,但是当我尝试将文件保存回时,将引发“无权限”错误。 I cannot relocate the file. 我无法重定位文件。 I'm stuck with this legacy schema so I'm not able to treat it like a normal .config file using ConfigurationManager.OpenExeConfiguration. 我受困于此旧架构,因此无法使用ConfigurationManager.OpenExeConfiguration将其视为普通的.config文件。 I cannot define sections or groups on this schema (I've not been able to anyway). 我无法在此架构上定义部分或组(无论如何我还是无法做到)。 All my users are Administrators on their local machines. 我所有的用户都是其本地计算机上的管理员。

How do I overwrite or delete and replace this file while it is in a protected directory(my assumption about the permissions error)? 当文件位于受保护的目录中时,如何覆盖或删除并替换该文件(我对权限错误的假设)? Failing that, is there a way to access this schema somehow with ConfigurationManager.OpenExeConfiguration. 失败的话,有没有一种方法可以使用ConfigurationManager.OpenExeConfiguration来访问此架构。

{edit starts here} {编辑从这里开始}

There are three applications in this scenario, A , B , and mine C . 在此方案中有三个应用程序AB和mine C。 Application A does not know about any other applications. 应用程序A不知道任何其他应用程序。 It can connect to many, many databases, and it drops a single file 'middle.config' that contains pointer info to the last database location that was used by the last Application A session. 它可以连接到许多数据库,并且删除一个文件“ middle.config”,其中包含指向上一个Application A会话使用的上一个数据库位置的指针信息。 Application B , let's call it an import/export application, only operates on the last Application A database location. 应用程序B (我们将其称为导入/导出应用程序)仅在最后一个应用程序A数据库位置上运行。 Application B reads the 'middle.config' file for database pointer info and then executes console commands against that database. 应用程序B读取“ middle.config”文件以获取数据库指针信息,然后针对该数据库执行控制台命令。 It performs bulk dumps or bulk imports for selected portions of the database. 它为数据库的选定部分执行批量转储或批量导入。

This was the situation when I come along to build application C that uses the import/export application B to fetch, blocks of data and return them to the database. 当我开始构建使用导入/导出应用程序B来获取数据块并将其返回到数据库的应用程序C时 ,就是这种情况。 So, in order for application C to use Application B against any database, application C must modify the 'middle.config' file so that application B will find the correct database. 因此,为了使应用程序C对任何数据库使用应用程序B ,应用程序C必须修改“ middle.config”文件,以便应用程序B可以找到正确的数据库。 Application C is new and the other two are legacy. 应用程序C是新的,其他两个是旧的。 I either find a way to make this work, or I force the user to start Application A and point to the database of interest, then close Application A . 我要么找到一种方法来完成这项工作,要么强迫用户启动Application A并指向感兴趣的数据库,然后关闭Application A。 This is VERY unhandy. 这非常不方便。

{edit ends here} {编辑到此为止}

It is not advisable to write data files to the program files directory, as this requires elevated permissions. 不建议将数据文件写入程序文件目录,因为这需要提升的权限。 Giving a program elevated permissions just to update a config file clashes with the Principal of Least Privilege , which states that 授予程序仅用于更新配置文件的提升权限与Least Privilege原则冲突,该原则规定:

a particular abstraction layer of a computing environment, every module (such as a process, a user or a program depending on the subject) must be able to access only the information and resources that are necessary for its legitimate purpose 计算环境的特定抽象层,每个模块(例如,取决于主题的过程,用户或程序)必须只能访问对其合法目的必要的信息和资源

It's not a "legitimate purpose" to give the process elevated permissions (that can allow it to do many harmful things) just to update a config file. 仅仅为了更新配置文件而赋予进程更高的权限(这可以允许它做很多有害的事情)不是“合法目的”。 MS recommended practice is to write that type of data elsewhere. MS建议的做法是将这种类型的数据写入其他位置。

Instead, consider storing the config file in a subfolder of the ApplicationData folder . 而是考虑将配置文件存储在ApplicationData文件夹的子文件夹中

Your assumption about protected directories is correct. 您对受保护目录的假设是正确的。 Program Files has an Access Control List which prevents modification by processes running as standard users and, on Vista upwards, even by administrator processes which are not running elevated. 程序文件具有访问控制列表,该列表可以防止以标准用户身份运行的进程以及在Vista或更高版本上甚至由未运行提升权限的管理员进程进行的修改。 Accessing the file using the standard configuration classes would not get around this. 使用标准配置类访问文件将无法解决此问题。

If you absolutely can't move the file (Eric J. is right when says that writing to Program Files after installation is a bad idea), then you could embed a manifest in your config file-editing program which will try to force elevation with a UAC prompt at launch . 如果您绝对不能移动文件(Eric J.说的对,即在安装后写入Program Files是个坏主意,那是对的),则可以将清单嵌入到配置文件编辑程序中,该程序将尝试强制使用启动时出现UAC提示 Of course, the best solution would involve a) using standard config schema and b) keeping user data in user-writeable locations, but sometimes that isn't possible for legacy reasons. 当然,最好的解决方案将包括:a)使用标准配置方案和b)将用户数据保存在用户可写的位置,但是有时由于遗留原因而无法实现。

I'm not aware of any way to persuade ConfigurationManager to read a non-standard schema, unfortunately. 不幸的是,我不知道有什么方法可以说服ConfigurationManager读取非标准架构。

建议您的应用为当前用户在AppData位置文件夹下创建自己的位置,而不是在应用安装位置下写入文件(特别是在程序文件下,这非常严格。)不建议强迫用户执行以下操作:或者以管理员身份运行您的应用程序。

将逻辑移到一个单独的进程,并以您当前应用程序的管理员权限启动它。

From a different angle, look at this: Writing custom sections into app.config 从另一个角度来看: 将自定义部分写入app.config

I found the linked article to be very useful. 我发现链接的文章非常有用。 Not sure it is going to answer all your questions though. 不确定是否会回答您所有的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM