我是要立刻拔掉所有的头发。不用说,我很沮丧。我在Delphi Prism for .NET中进行编码,目前正在努力将我的应用程序的用户定义属性保存到Application配置文件中。我按如下方式设置了所有内容。它正在工作,但根本没有保存或检索某些值。所以,我查看了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文件中看到的不匹配。
答案 0 :(得分:4)
用户设置是指可以基于每个用户更改/保存的设置。因此,他们不保存在您的app.config中,而是保存在名为user.config的新文件中,该文件保存在de%APPDATA%目录下。在Windows 7机器上它可能看起来像
C:\用户\名为myUsername \应用程序数据\本地\微软\ myassemblyname \ user.config (在某些我无法回忆的地方可能还会有一些指导)
也可以在app.config中找到用户设置,但这些值在首次创建新的user.config文件时用作默认值。
编辑:有关查找文件的额外信息。我在vs调试器中运行了一个名为DEA.exe的程序集的快速测试,user.config最终进入:
C:\用户\\应用程序数据\本地\\ DEA.vshost.exe_Url_sloja2hil2eiypecefjbp1icc1fo50g4 \\ user.config
在this msdn文件中可以找到更多信息(具体说明的最后一段)
答案 1 :(得分:0)
我不知道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;
}
}
我认为Delphi会生成一个类似的文件
默认值被编码为属性,如果在app.config中未指定任何内容,则使用默认值。没有任何内容写入app.config
在c#情况下,设计器还尝试在app.config中创建相应的默认值,但如果删除它们,则使用DefaultSettingValueAttribute
中的默认值。
如果在app.config中手动创建一个值,是否使用了该值?