david's daily developer note

C# 다수의 Process 실행하기. 경로 관리 본문

[Develop] Language/C#

C# 다수의 Process 실행하기. 경로 관리

mouse-david 2011. 1. 19. 12:02
728x90
특정 경로의 파일 스트림을 읽는 서브 프로그램이 있다. 서브 프로그램을 다수 실행하도록 동작하는 메인 프로그램이 있을 때,
서브 프로그램의 파일 경로는 어떻게 될까?
다음은 메인 프로그램이다. 현재 자신의 디렉토리에 존재하는 "queryMachin.exe"를 수행한다.

            try
            {
                string mainPath = Environment.CurrentDirectory + "\\queryMachin\\bin\\Debug\\queryMachin.exe";
                Process.Start(mainPath);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

다음은 서브 프로그램으로, 마찬가지 자신의 디렉토리(Debug)에 존재하는 test.txt 파일을 읽는다.

             try 
            {
                string currentPath = Environment.CurrentDirectory;
                
                Encoding euckrEncode = Encoding.GetEncoding("euc-kr");
                StreamReader sr = new StreamReader(currentPath+"\\queryMachin\\bin\\Debug\\test.txt", euckrEncode);

                Console.WriteLine(sr.ReadLine());    
            }
            catch(IOException ex)
            {
                Console.WriteLine(ex.ToString());
            }

사실 서브 프로그램 하나만을 돌릴 경우에는 위의 소스의 스트림 리더를 생성하는 부분이 아래와 같다.
StreamReader sr = new StreamReader(currentPath+"\\test.txt", euckrEncode);

하지만  메인 프로그램이 서브 프로그램을 하나의 프로세스로서 동작하게 만드는 위의 경우에서는
서브 프로그램과 메인프로그램의 Environment.CurrentDirectory는 서로 같다. 때문에, 메인 프로그램에서 
파일 처리를 자동으로 하는 서브 프로그램을 수행하려면, 이 같은 사항을 고려해야 고려하자. - 약간 신경쓰이는 문제

그 밖에 Process 클레스에 대한 내용은 아래 MSDN을 참조하면 되겠다.

참조 : http://msdn.microsoft.com/ko-kr/library/system.diagnostics.process(VS.90).aspx
728x90