2014-04-07 2 views
2

이것은 settings.settings 파일에 새로운 설정을 추가하기 위해 사용하고 있지만 작동하지 않는 코드입니다.Settings.setting 파일에 새 설정 추가하기

System.Configuration.SettingsProperty property = new System.Configuration.SettingsProperty("CustomSetting"); 

property.DefaultValue = "Default"; 
property.IsReadOnly = false; 
property.PropertyType = typeof(string); 
property.Provider = Properties.Settings.Default.Providers["LocalFileSettingsProvider"]; 
property.Attributes.Add(typeof(System.Configuration.UserScopedSettingAttribute), new System.Configuration.UserScopedSettingAttribute()); 

Properties.Settings.Default.Properties.Add(property); 
// Load settings now. 
Properties.Settings.Default.Reload(); 
// Update the user itnerface. 

Properties.Settings.Default["CustomSetting"] = txt_Cipher.Text; 
Properties.Settings.Default.Save(); 
txt_Cipher.Text = string.Empty; 

새로운 설정이 설정 파일에 추가되지 않았습니다. 이것은 내가 sumesh의 회신에 따라 노력한 새로운 코드이다.

System.Configuration.SettingsProperty property = new System.Configuration.SettingsProperty("CI"); 
       Properties.Settings.Default["CI"] = txt_Cipher.Text; 
       Properties.Settings.Default.Save(); 
       Properties.Settings.Default.Reload(); 
       txt_Cipher.Text = string.Empty; 

이미 sumesh의 이미지에서 설명한대로 settings.setting 파일에 설정을 만들었습니다.

+0

코드가 예외를 throw합니까? – Sumeshk

+0

아니,하지만 난 그 설정 파일에 변경 사항을 볼 수 없습니다 – wintersolider

+0

일단 그것이 추가되면 CustomSetting가 추가 된 것 같아요 Properties.Settings.Default.Properties.Add (속성); 열쇠가 벌써 존재하는 에러를 슬로우합니다. – Sumeshk

답변

1

파일을 수동으로 settings.settings에서 설정을 업데이트 할 수

System.Configuration.SettingsProperty property = new System.Configuration.SettingsProperty("CustomSetting"); 


Properties.Settings.Default["CustomSetting"] = txt_Cipher.Text; 
Properties.Settings.Default.Save(); 
txt_Cipher.Text = string.Empty; 

사용이 코드를 CustomSetting 추가

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <userSettings> 
     <WindowsFormsApplication1.Properties.Settings> 
      <setting name="CustomSetting" serializeAs="String"> 
       <value>1</value> 
      </setting> 
     </WindowsFormsApplication1.Properties.Settings> 
    </userSettings> 
</configuration> 
으로이 설정을 만들어 저와 XML 파일에 대한 제대로 작동

enter image description here

쾌감 e 그림과 같이 customsSettings를 추가하십시오.

+0

사용자 정의 설정을 추가하는 동안 범위는 무엇입니까? – wintersolider

+0

우리는 사용자 수준과 응용 프로그램 수준 범위를 설정할 수 있습니다 – Sumeshk

+0

하지만 응용 프로그램 수준으로 범위를 설정하면 설정이 단지 – wintersolider