2013-05-31 3 views
0

내 프로그램에서 파일 명명 규칙을 구현하려고 시도했습니다.구성 파일에서 파일 이름 지정 규칙 처리

: - 프로그램 구성 파일을 읽고 프로그램에 다음과 같이 문자열을 포맷 할 것이다 예를 들어, 그래서

MyConfig.conf 
# File naming convention for output-file 
[Field1][Field3][Field2] 

프로그램 내에서 문자열 'FieldX'대응 예를 들어 I는 다음과 같이 구성 파일이

Field1Value Field2Value Field3Value 

C#에서 이런 종류의 작업을 수행하는 기본 방법이 있습니까?

+0

당신은 FieldX는 임의의 문자열로 구성 할 수 있습니다 의미? FieldX를 재배치/포맷하면 파일 이름을 만들 수 있습니까? 또는 재배치가 구성 가능해야 함을 의미합니까? – aiapatag

답변

1

내가 생각할 수있는 가장 쉬운 방법은 앱 설정을 사용하는 것입니다. 앱 설정에는 필요한 문자열 형식이 포함되어 있습니다. 그런 다음 그 문자열 형식을 사용합니다.

using System; 
using System.Collections.Generic; 
using System.Collections.Specialized; 
using System.Configuration; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace _16852548 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      NameValueCollection appSettings = ConfigurationManager.AppSettings; 

      string field1Value = "Filename"; 
      string field2Value = "."; 
      string field3Value = "txt"; 

      string fileFormat = appSettings["FileNameFormat"]; 

      Console.WriteLine(string.Format(fileFormat, field1Value, field2Value, field3Value)); 
     } 
    } 
} 

그런 다음 설정 파일은 다음과 같습니다

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <appSettings> 
    <add key="FileNameFormat" value="{0}{2}{1}"/> <!-- follow string.Format syntax --> 
    </appSettings> 
</configuration>