简体   繁体   English

实际保存或写入的应用程序属性设置在哪里?

[英]Where are the application properties settings actually saved or written to?

I am about pull all of my hair at once. 我是要立刻拔掉所有的头发。 Needless to say, I am frustrated. 不用说,我很沮丧。 I am coding within Delphi Prism for .NET and currently working to get my application's user-defined properties saved into Application config file. 我在Delphi Prism for .NET中进行编码,目前正在努力将我的应用程序的用户定义属性保存到Application配置文件中。 I have setup everything as follows. 我按如下方式设置了所有内容。 It is working but some Values are not being saved or retrieved at all. 它正在工作,但根本没有保存或检索某些值。 So, I looked into the app.config file thinking they will all be there, but I didn't see any of the values saved in the file match the values that is being retrieved. 所以,我查看了app.config文件,认为它们都将存在,但我没有看到文件中保存的任何值与正在检索的值匹配。 It only has default values I put into Properties.Designer. 它只有我放入Properties.Designer的默认值。 Someone did say online that config file will only be updated when you run your application outside of Delphi Prism IDE. 有人在网上说,只有在Delphi Prism IDE之外运行应用程序时才会更新配置文件。 And I did, that still didn't work. 而我做了,仍然没有奏效。 I even rebooted my computer thinking that maybe Delphi Prism is flaky and it needed a quick break. 我甚至重新启动计算机,认为Delphi Prism可能很脆弱,需要快速休息。 Even that didn't work. 即使这样也行不通。

So, where are the values being saved - in app.config file or in memory? 那么,保存的值在哪里 - 在app.config文件中或在内存中?

(my code might be choppy. I am trying to get the basic setup for my app working before I polish the code.) (我的代码可能不稳定。我正在尝试在我修改代码之前为我的应用程序工作的基本设置。)

Here is the image of my Properties.Settings.Designer: 这是我的Properties.Settings.Designer的图像:

Properties.Settings.Designer

Here is the app.config file: 这是app.config文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="MillenniaMono.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <userSettings>
        <MillenniaMono.Properties.Settings>
            <setting name="baseDir" serializeAs="String">
                <value>00:00:00</value>
            </setting>
            <setting name="SoftKey" serializeAs="String">
                <value>$6e1eaffa</value>
            </setting>
            <setting name="OnTop" serializeAs="String">
                <value>False</value>
            </setting>
            <setting name="StartExpanded" serializeAs="String">
                <value>True</value>
            </setting>
            <setting name="HideOnStart" serializeAs="String">
                <value>False</value>
            </setting>
            <setting name="DXCommLocation" serializeAs="String">
                <value>50, 50</value>
            </setting>
            <setting name="InputBoxLocation" serializeAs="String">
                <value>50, 50</value>
            </setting>
            <setting name="MainFormLocation" serializeAs="String">
                <value>50, 50</value>
            </setting>
            <setting name="OutputUnitLocation" serializeAs="String">
                <value>50, 50</value>
            </setting>
            <setting name="SysErrorDlgLocation" serializeAs="String">
                <value>50, 50</value>
            </setting>
            <setting name="UnitDuplicateLocation" serializeAs="String">
                <value>50, 50</value>
            </setting>
            <setting name="UnitsLocation" serializeAs="String">
                <value>50, 50</value>
            </setting>
            <setting name="UnitSelectLocation" serializeAs="String">
                <value>50, 50</value>
            </setting>
            <setting name="CommStatusLocation" serializeAs="String">
                <value>50, 50</value>
            </setting>
            <setting name="UnitsWinSize" serializeAs="String">
                <value>703, 300</value>
            </setting>
            <setting name="DXComm_TypeDX" serializeAs="String">
                <value>0</value>
            </setting>
            <setting name="DXComm_Comport" serializeAs="String">
                <value>0</value>
            </setting>
            <setting name="DXComm_Baud" serializeAs="String">
                <value>1200</value>
            </setting>
            <setting name="DXComm_Parity" serializeAs="String">
                <value>0</value>
            </setting>
            <setting name="DXComm_RetryInt" serializeAs="String">
                <value>0</value>
            </setting>
            <setting name="DXComm_TimeOutDelay" serializeAs="String">
                <value>0</value>
            </setting>
            <setting name="DXComm_ScanTime" serializeAs="String">
                <value>0</value>
            </setting>
            <setting name="DXComm_Offline_Pct" serializeAs="String">
                <value>0</value>
            </setting>
            <setting name="DXComm_Online_Pct" serializeAs="String">
                <value>0</value>
            </setting>
            <setting name="DXComm_ControlCycleCount" serializeAs="String">
                <value>0</value>
            </setting>
            <setting name="DXComm_LockTimeDelay" serializeAs="String">
                <value>0</value>
            </setting>
        </MillenniaMono.Properties.Settings>
    </userSettings>
</configuration>

Here is the code for writing into app.config: 这是写入app.config的代码:

  if ReadWrite then
  begin
      RetryIni := Int32(RetrySpin.value);
      OfflinePct := int32(OfflineSpin.value);
      ScanTime := int32(ScanSpin.value);
      OnlinePct := int32(OnlineSpin.value);
      timeoutDelay := int32(TimeoutSpin.Value);
      ControlCycleCount := int32(ControlSpin.value);
      LockTimeDelay := Int32(LockTime.value);

      with commsetting := Millenniamono.Properties.Settings.Default do
      begin
        commsetting.DXComm_TypeDX := TypeDXCard.SelectedIndex;
        commsetting.DXComm_Comport := Commport.SelectedIndex;
        commsetting.DXComm_Baud:=BaudRate.SelectedIndex;
        commsetting.DXComm_Parity := int32(EvenParity.Checked = true);
        commsetting.DXComm_RetryInt:=RetryIni;
        commsetting.DXComm_TimeOutDelay:=timeOutDelay;
        commsetting.DXComm_ScanTime:=ScanTime;
        commsetting.DXComm_Offline_Pct:=OfflinePct;
        commsetting.DXComm_Online_Pct:=onlinepct;
        commsetting.DXComm_ControlCycleCount:=ControlCycleCount;
        commsetting.DXComm_LockTimeDelay:=LockTimeDelay;
        commsetting.Save;
      end;
  end;

Here is the code reading: 这是代码阅读:

TypeDXCard.SelectedIndex:= commsetting.DXComm_TypeDX;
Commport.SelectedIndex:=CommSetting.DXComm_Comport;

case commsetting.DXComm_Baud of
1200: BaudRate.SelectedIndex:=0;
2400: BaudRate.SelectedIndex:=1;
4800: Baudrate.SelectedIndex:=2;
9600: BaudRate.SelectedIndex:=3;
19200: BaudRate.SelectedIndex:=4;
38400: BaudRate.SelectedIndex:=5;
57600: BaudRate.SelectedIndex:=6;
115200: BaudRate.SelectedIndex:=7;
else BaudRate.SelectedIndex:=0;
end;

case commsetting.DXComm_Parity of 
0: begin EvenParity.Checked:=true; NoneParity.Checked:=false; end;
1: begin NoneParity.Checked:=true; EvenParity.Checked:=false; end;
else begin EvenParity.Checked:=true; NoneParity.Checked:=false; end;
end;

RetrySpin.Value:=Commsetting.DXComm_RetryInt;
RetryIni := RetrySpin.Value;

TimeOutSpin.Value:=CommSetting.DXComm_TimeOutDelay;
TimeOutDelay := TimeOutSpin.Value;

ScanSpin.Value:=CommSetting.DXComm_ScanTime;
ScanTime := ScanSpin.Value;

OfflineSpin.Value:=CommSetting.DXComm_Offline_Pct;
OfflinePct:=OfflineSpin.Value;

OnlineSpin.Value:=Commsetting.DXComm_Online_Pct;
OnlinePct:=OnlineSpin.Value;

Controlspin.Value:=Commsetting.DXComm_ControlCycleCount;
ControlCycleCount:=Controlspin.Value;

LockTime.Value:=Commsetting.DXComm_LockTimeDelay;
LockTimeDelay := LockTime.Value;
  end;

Here is the actual values being displayed or retrieved. 以下是显示或检索的实际值。 I arbitrarily set some values to be saved and read back but it doesn't match what I am seeing in app.config file. 我随意设置了一些值来保存和回读,但它与我在app.config文件中看到的不匹配。

窗口

User settings are meant as settings that can be altered/saved on a per-user basis. 用户设置是指可以基于每个用户更改/保存的设置。 Therefor they are not saved in your app.config but in a new file called user.config and that file is saved under de %APPDATA% directory. 因此,它们不会保存在您的app.config中,而是保存在名为user.config的新文件中,该文件保存在de%APPDATA%目录下。 On a windows 7 machine it probably looks something like 在Windows 7机器上它可能看起来像

c:\\users\\myusername\\appdata\\local\\microsoft\\myassemblyname\\user.config (there might also be some guid somewhere I can't recall) c:\\ users \\ myusername \\ appdata \\ local \\ microsoft \\ myassemblyname \\ user.config(可能还有一些我无法回忆的guid)

The user settings can also be found in the app.config but those values are used as the default values when a new user.config file is created for the first time. 用户设置也可以在app.config中找到,但这些值在第一次创建新的user.config文件时用作默认值。

EDIT: Extra info on locating the file. 编辑:有关查找文件的额外信息。 I did a quick test running in the vs debugger for an assembly called DEA.exe and the user.config ended up in: 我在vs调试器中运行了一个名为DEA.exe的程序集的快速测试,user.config最终进入:

C:\\Users\\\\AppData\\Local\\\\DEA.vshost.exe_Url_sloja2hil2eiypecefjbp1icc1fo50g4\\\\user.config C:\\用户\\\\应用程序数据\\本地\\\\ \\\\ DEA.vshost.exe_Url_sloja2hil2eiypecefjbp1icc1fo50g4 user.config

A bit more info can be found in this msdn document (last paragraph on the remarks specifically) 这个 msdn文档中可以找到更多的信息(特别是备注的最后一段)

I don't know about Delphi, but at least c# generates a real code file from the settings designer, with content like this 我不知道Delphi,但至少c#从设置设计器生成一个真实的代码文件,内容是这样的

[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("foo")]
public string bar {
    get {
        return ((string)(this["bar"]));
    }
    set {
        this["bar"] = value;
    }
}

I assume Delphi will generate a similar file. 我假设Delphi会生成一个类似的文件。
The default value is encoded as an attribute, if nothing is specified in app.config the default value is used. 默认值被编码为属性,如果在app.config中未指定任何内容,则使用默认值。 Nothing is written to app.config. 没有任何内容写入app.config。
In the c# case the designer also tries to create corresponding default values in app.config, but if they are removed the default value from the DefaultSettingValueAttribute is used. 在c#情况下,设计器还尝试在app.config中创建相应的默认值,但如果删除它们,则使用DefaultSettingValueAttribute的默认值。

If you manually create a value in app.config, is that value used? 如果在app.config中手动创建值,是否使用了该值?

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

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