2

많은 사용자 범위 설정이 ApplicationSettingsBase에서 상속 한 개체별로 user.config에 저장되어 있습니다.버전 번호가 변경되면 user.config의 모든 설정을 어떻게 업그레이드합니까?

각 인스턴스의 SettingsKey은 주로 양식 이름을 사용하여 런타임에 동적으로 파생됩니다. 따라서 수백 가지가있을 수 있습니다.

많은 질문과 답변 (예 : How do you keep user.config settings across different assembly versions in .net?)을 읽으면서 모두 버전 번호 확인에서 ApplicationSettingsBase.Upgrade() 호출을 권장합니다.

문제는 인스턴스화하기 위해 사용되는 매 * SettingsKey (값을 알 필요가 (내가 말할 수있는까지)는 모든 ApplicationSettingsBase 차례로 업그레이드 방법을 호출하는 객체.

거기인가 방법으로 모든 user.config 설정을 한 번에 업그레이드하거나 파일의 모든 설정을 반복하여 업그레이드 할 수 있습니까?

+0

XML 파일 일뿐입니다. –

+1

예 제레미 Xml이고, 오늘 아침에 시작한 트랙입니다. 그것은 Xml 파일이지만 새 버전의 응용 프로그램마다 사용자 프로필 내의 새 폴더에 새 Xml 파일이 만들어집니다. 현재, 나는 오래된 XML 파일을 찾고 모든 관련 설정 객체를 인스턴스화하기 위해 userSettings 노드를 읽는 중이다. 그것은 "해커"를 느낀다. 그리고 나는 더 좋은 길이 있는지 궁금하게 생각하고있다. (내가이 트랙을 시작하지 않았다는 것을 질문했을 때). 더 나은 것이 제시되지 않으면 결과를 게시 할 것입니다. –

+0

나는 이것이 http://stackoverflow.com/questions/534261/how-do-you-keep-user-config-settings-across-different-assembly-versions-in-net의 사본이라고 생각한다. 여기에 수락 된 대답보다 더 나은 대답). –

답변

1

내가 생각한 접근 방식은 내가 느끼는 해킹 방법이지만 너무 많은 접근 방식이 실패했습니다. 그리고 나는 일들을 계속해야합니다 :-(

새 버전이 실행될 때 이전 버전의 user.config를 복사했습니다.

먼저이 질문의 많은 변형이 권장하는 것처럼 업그레이드가 필요한지 여부를 결정하십시오. 다음

System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly(); 
Version version = assembly.GetName().Version; 

if (version.ToString() != Properties.Settings.Default.ApplicationVersion) 
{ 
    copyLastUserConfig(version); 
} 

는 PreviousSettingsDir를 얻을 중간에 마지막 user.config ....이 질문에 ( How do you upgrade Settings.settings when the stored data type changes?)에 그의 대답에 Allon Guralnek

private static void copyLastUserConfig(Version currentVersion) 
{ 
try 
{ 
    string userConfigFileName = "user.config"; 


    // Expected location of the current user config 
    DirectoryInfo currentVersionConfigFileDir = new FileInfo(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath).Directory; 
    if (currentVersionConfigFileDir == null) 
    { 
     return; 
    } 

    // Location of the previous user config 

    // grab the most recent folder from the list of user's settings folders, prior to the current version 
    var previousSettingsDir = (from dir in currentVersionConfigFileDir.Parent.GetDirectories() 
           let dirVer = new { Dir = dir, Ver = new Version(dir.Name) } 
           where dirVer.Ver < currentVersion 
           orderby dirVer.Ver descending 
           select dir).FirstOrDefault(); 

    if (previousSettingsDir == null) 
    { 
     // none found, nothing to do - first time app has run, let it build a new one 
     return; 
    } 

    string previousVersionConfigFile = string.Concat(previousSettingsDir.FullName, @"\", userConfigFileName); 
    string currentVersionConfigFile = string.Concat(currentVersionConfigFileDir.FullName, @"\", userConfigFileName); 

    if (!currentVersionConfigFileDir.Exists) 
    { 
     Directory.CreateDirectory(currentVersionConfigFileDir.FullName); 
    } 

    File.Copy(previousVersionConfigFile, currentVersionConfigFile, true); 

} 
catch (Exception ex) 
{ 
    HandleError("An error occurred while trying to upgrade your user specific settings for the new version. The program will continue to run, however user preferences such as screen sizes, locations etc will need to be reset.", ex); 
} 
} 

감사 Linq에 대한 복사합니다.