应用程序属性设置实际保存或写入到哪里?
我马上就要拉掉我所有的头发。不用说,我很沮丧。我正在 Delphi Prism for .NET 中进行编码,目前正在努力将应用程序的用户定义属性保存到应用程序配置文件中。我已将所有内容设置如下。它正在工作,但某些值根本没有被保存或检索。因此,我查看了 app.config 文件,认为它们都会在那里,但我没有看到文件中保存的任何值与正在检索的值相匹配。它只有我放入 Properties.Designer 中的默认值。有人确实在网上说过,只有当您在 Delphi Prism IDE 之外运行应用程序时,配置文件才会更新。我也这么做了,但还是没用。我什至重新启动了计算机,认为 Delphi Prism 可能不稳定,需要快速休息一下。即使这样也行不通。
那么,这些值保存在哪里 - app.config 文件还是内存中?
(我的代码可能不稳定。我试图在完善代码之前让我的应用程序进行基本设置。 )
这是我的 Properties.Settings.Designer 的图像:
<强>这是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>
这是写入 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;
这里是读取的代码:
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;
这里是显示或检索的实际值。我任意设置了一些要保存和读回的值,但它与我在 app.config 文件中看到的不匹配。
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. 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. It only has default values I put into Properties.Designer. Someone did say online that config file will only be updated when you run your application outside of 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. Even that didn't work.
So, where are the values being saved - in app.config file or in memory?
(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:
Here is the app.config file:
<?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:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用户设置是指可以针对每个用户更改/保存的设置。因此,它们不保存在您的 app.config 中,而是保存在名为 user.config 的新文件中,并且该文件保存在 de %APPDATA% 目录下。在 Windows 7 机器上,它可能看起来像这样
用户设置也可以在 app.config 中找到,但当第一次创建新的 user.config 文件时,这些值将用作默认值。
编辑:有关查找文件的额外信息。 集进行了快速测试,user.config 最终位于:
C:\Users\\AppData\Local\\DEA.vshost.exe_Url_sloja2hil2eiypecefjbp1icc1fo50g4\\user.config
我在 vs 调试器中对名为 DEA.exe 的程序 信息可以在 这个 msdn文档(最后一段专门针对备注)
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. On a windows 7 machine it probably looks something like
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.
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:
C:\Users\\AppData\Local\\DEA.vshost.exe_Url_sloja2hil2eiypecefjbp1icc1fo50g4\\user.config
A bit more info can be found in this msdn document (last paragraph on the remarks specifically)
我不了解 Delphi,但至少 c# 从设置设计器生成一个真实的代码文件,内容如下,
我假设 Delphi 会生成一个类似的文件。
默认值被编码为属性,如果 app.config 中未指定任何内容,则使用默认值。 app.config 中未写入任何内容。
在 C# 情况下,设计器还会尝试在 app.config 中创建相应的默认值,但如果删除它们,则使用
DefaultSettingValueAttribute
中的默认值。如果您在 app.config 中手动创建一个值,是否会使用该值?
I don't know about Delphi, but at least c# generates a real code file from the settings designer, with content like this
I assume Delphi will generate a similar file.
The default value is encoded as an attribute, if nothing is specified in app.config the default value is used. Nothing is written to 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.If you manually create a value in app.config, is that value used?