developer tip

App.config : 사용자 대 애플리케이션 범위

optionbox 2020. 9. 19. 10:53
반응형

App.config : 사용자 대 애플리케이션 범위


내 프로젝트에 App.config 파일을 추가했습니다. 프로젝트> 속성> 설정 패널에서 두 가지 설정을 만들었습니다.

여기에 이미지 설명 입력

설정을 추가 할 때 범위를 User또는 로 정의 할 수 있습니다 Application. -

  1. 사용자
  2. 신청

I는 다음과 같이 설정 정의하는 경우 User가 간다 userSettings, 섹션
내가 설정을 정의하는 경우로 Application이가는 applicationSettings부분

App.config

<configuration>

    <userSettings>
        <DemoApp.Properties.Settings>
            <setting name="MySetting1" serializeAs="String">
                <value>Value1</value>
            </setting>
        </DemoApp.Properties.Settings>
    </userSettings>

    <applicationSettings>
        <DemoApp.Properties.Settings>
            <setting name="MySetting2" serializeAs="String">
                <value>Value2</value>
            </setting>
        </DemoApp.Properties.Settings>
    </applicationSettings>

</configuration>

그러나, 이러한 설정에서 같은 방법으로 액세스 할 수 있습니다 .cs-

암호

string mySetting1 = DemoApp.Properties.Settings.Default.MySetting1;
string mySetting2 = DemoApp.Properties.Settings.Default.MySetting2;

UserApplication범위 의 차이점은 무엇이며 어떤 상황에서이 둘 중에서 선택해야합니까?


Basically, application settings cannot be changed during the running of a program and user settings can. These user settings should then be saved so the user is presented with a familiar experience when (s)he runs the application next.

Edit: For examples, you might write your application with different modules, and need to ensure that your main module is using the correct version of your security module. For this you would set up an application-scope setting eg:

SecurityModuleVersion  string     Application      v1.21

Sometime later when you refactor the security module, you might change the value to v1.22 when you deploy to ensure the correct security is being implemented

On the other hand, if your application has different 'skins' with color changes, font changes etc, then you may setup a user setting something like the following:

ApplicationSkin        string     User              DefaultSkin

Then, when Michelle changes to the skin she prefers, the application remembers her settings. The properties may now look like:

ApplicationSkin        string     User              HelloKittySkin

Application-scope settings are read only, and can only be changed at design time or by altering the .exe.config file in between application sessions. User-scope settings, however, can be written at run time, just as you would change any property value. The new value persists for the duration of the application session. You can persist changes to user settings between application sessions by calling the Settings.Save method.

Source on msdn: Using Settings in C#

User settings are generally of use for persisting user preferences (e.g. app notification preferences etc.). Application settings would generally for items such as API keys etc.

@kmote에서 언급했듯이 사용자 설정이 런타임에 수정되고 유지되면 (을 통해 settings.Save()) 사용자 프로필 저장소 내의 폴더 (일반적으로 Windows 7 이상에서는 C : \ Users \ Username \ AppData \ Local \ AppName)에 기록 됩니다. ). 프로그래밍 방식으로 파일의 위치를 ​​확인하려면 이 게시물을 참조하십시오 .

참고 URL : https://stackoverflow.com/questions/13046907/app-config-user-vs-application-scope

반응형