다른 Exe의 App.Config 가져오기
-
09-06-2019 - |
문제
나는 exe를 가지고 있습니다. App.Config
파일.이제 일부 기능을 사용하기 위해 exe 주위에 래퍼 dll을 만들고 싶습니다.
문제는 래퍼 dll에서 exe의 app.config 속성에 어떻게 액세스할 수 있느냐는 것입니다.
어쩌면 내 질문에 좀 더 많은 내용을 추가해야 할 수도 있습니다. exe와 함께 다음 app.config 콘텐츠가 있습니다.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="myKey" value="myValue"/>
</appSettings>
</configuration>
문제는 래퍼 dll에서 "myValue"를 가져오는 방법입니다.
귀하의 솔루션에 감사드립니다.
사실 내 초기 개념은 XML 파일 읽기 방법이나 LINQ 등을 피하는 것이었습니다.내가 선호하는 솔루션은 구성 관리자 라이브러리 등.
일반적으로 app.config 속성 액세스와 관련된 클래스를 사용하는 데 도움을 주시면 감사하겠습니다.
해결책 2
몇 가지 테스트를 거친 후 이를 수행할 수 있는 방법을 찾았습니다.
- App.Config 파일을 테스트 프로젝트에 추가합니다."링크로 추가" 옵션을 사용하세요.
- 사용
System.Configuration.ConfigurationManager.AppSettings["myKey"]
값에 액세스합니다.
다른 팁
그만큼 ConfigurationManager.OpenMappedExeConfiguration 메서드 당신이 이것을 할 수있게 해줄 것입니다.
MSDN 페이지의 샘플:
static void GetMappedExeConfigurationSections()
{
// Get the machine.config file.
ExeConfigurationFileMap fileMap =
new ExeConfigurationFileMap();
// You may want to map to your own exe.comfig file here.
fileMap.ExeConfigFilename =
@"C:\test\ConfigurationManager.exe.config";
System.Configuration.Configuration config =
ConfigurationManager.OpenMappedExeConfiguration(fileMap,
ConfigurationUserLevel.None);
// Loop to get the sections. Display basic information.
Console.WriteLine("Name, Allow Definition");
int i = 0;
foreach (ConfigurationSection section in config.Sections)
{
Console.WriteLine(
section.SectionInformation.Name + "\t" +
section.SectionInformation.AllowExeDefinition);
i += 1;
}
Console.WriteLine("[Total number of sections: {0}]", i);
// Display machine.config path.
Console.WriteLine("[File path: {0}]", config.FilePath);
}
편집하다:그러면 "myKey" 값이 출력됩니다.
ExeConfigurationFileMap fileMap =
new ExeConfigurationFileMap();
fileMap.ExeConfigFilename =
@"C:\test\ConfigurationManager.exe.config";
System.Configuration.Configuration config =
ConfigurationManager.OpenMappedExeConfiguration(fileMap,
ConfigurationUserLevel.None);
Console.WriteLine(config.AppSettings.Settings["MyKey"].Value);
내 생각에 당신이 찾고 있는 것은 다음과 같습니다.
System.Configuration.ConfigurationManager.OpenExeConfiguration(string path)
나는 다른 방법이 있다는 Gishu의 주장에 두 번째로 동의합니다.EXE의 공통/"공개" 부분을 DLL로 추상화하고 이를 실행할 래퍼 EXE를 만드는 것이 더 낫지 않을까요?이것은 확실히 더 일반적인 개발 패턴입니다.사용하려는 항목만 DLL에 들어가고, EXE는 DLL에 들어간 작업을 제외하고 현재 수행하는 모든 작업을 수행합니다.
이는 XML 파일이므로 Linq-XML 또는 DOM 기반 접근 방식을 사용하여 관련 정보를 구문 분석할 수 있습니다.
(그렇다면 더 나은 디자인이 없을지 의문이 듭니다..당신은 달성하려고 노력하고 있습니다.)
IDE에 링크를 추가하면 개발 중에만 도움이 됩니다.나는 lomaxx의 생각이 옳다고 생각합니다. System.Configuration.ConfigurationManager.OpenExeConfiguration.