Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 1개 있습니다.)

Visual Studio Code에서 콘솔 응용 프로그램 개발 시 "입력"받는 방법

예를 들어 다음과 같이 입력을 받는 콘솔 프로그램을 만든 경우,

using System;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");

        Console.ReadLine();
    }
}

Visual Studio Code에서 실행하면 "입력"을 받을 수 없습니다. 물론 필요가 없다면 그냥 종료해도 되겠지만 그렇지 않은 경우라면 문제가 되는데요, 이에 대해서는 관련 옵션을 설정할 수 있는 다음의 공식 문서에 해답이 있습니다.

Configuring launch.json for C# debugging - Console (terminal) window
; https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window

Debug Console window cannot accept Console.ReadLine() input during debugging
; https://github.com/OmniSharp/omnisharp-vscode/issues/1053

그러니까, ".vscode/launch.json" 파일을 보면 기본적으로 "console" 설정이 입력을 받을 수 없는 "internalConsole"로 되어 있습니다.

{
   // Use IntelliSense to find out which attributes exist for C# debugging
   // Use hover for the description of the existing attributes
   // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
   "version": "0.2.0",
   "configurations": [
        {
            "name": ".NET Core Launch (console)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            // If you have changed target frameworks, make sure to update the program path.
            "program": "${workspaceFolder}/bin/Debug/netcoreapp3.0/vscode_console_input.dll",
            "args": [],
            "cwd": "${workspaceFolder}",
            // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
            "console": "internalConsole",
            "stopAtEntry": false
        },
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach",
            "processId": "${command:pickProcess}"
        }
    ]
}

이 값을 "integratedTerminal" 또는 "externalTerminal"로 바꿔주면 입력 값을 받을 수 있는데요. "externalTerminal"의 경우 실행 시 Visual Studio Code의 하단 "OUTPUT" 뷰가 사용되지 않고 아예 콘솔 창이 하나 뜨게 됩니다.

vscode_console_input_1.png

새 창이 하나 뜨는 게 왠지 거슬릴 수 있는데, 그런 경우라면 "integratedTerminal" 옵션을 선택하면 됩니다.




참고로 "Remote Development" 환경의 경우,

로컬의 Visual Studio Code로 원격 리눅스 머신에 접속해 개발하는 방법
; https://www.sysnet.pe.kr/2/0/11942

윈도우에서 리눅스로 접근하고 있다면, "externalTerminal" 설정은 다음과 같은 식의 오류가 발생하게 됩니다.

Unable to launch debugger worker process (vsdbg) through the terminal. can't find terminal application 'xterm'

xterm을 설치해 주면,

[Centos 7]

$sudo yum install xterm

이제는 다음과 같이 바뀝니다.

Unable to launch debugger worker process (vsdbg) through the terminal. xterm: Xt error: Can't open display: 

저도 해보진 않았는데 아마도 필요한 설정을 해주면,

Windows 10에서 리눅스용 프로그램 설치하고 실행하기
; https://medium.com/rkttu/windows-10%EC%97%90%EC%84%9C-%EB%A6%AC%EB%88%85%EC%8A%A4%EC%9A%A9-%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%A8-%EC%84%A4%EC%B9%98%ED%95%98%EA%B3%A0-%EC%8B%A4%ED%96%89%ED%95%98%EA%B8%B0-2cb0d7892d12

Xming X Server for Windows
; http://sourceforge.net/projects/xming/

"externalTerminal" 설정이 동작해 새 창으로 뜨게 될 것입니다. 하지만, "integratedTerminal" 옵션이 제공되므로 저렇게까지 할 필요는 없는 것 같습니다. "integratedTerminal" 옵션이 한 가지 불편한 점이 있다면, 그 모드로 띄웠을 때 예외가 발생해 콘솔 프로그램이 종료되면 "OUTPUT" 창이 더 이상 동작하지 않게 되는 문제가 발생한다는 점입니다. 이럴 때는 OUTPUT 창을 닫고 다시 열어야 하는 식으로 조정을 좀 해야 합니다.




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 7/5/2019]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... [46]  47  48  49  50  51  52  53  54  55  56  57  58  59  60  ...
NoWriterDateCnt.TitleFile(s)
12470정성태12/30/202010044.NET Framework: 991. .NET 5 응용 프로그램에서 WinRT API 호출 [1]파일 다운로드1
12469정성태12/30/202013636.NET Framework: 990. C# - SendInput Win32 API를 이용한 가상 키보드/마우스 [1]파일 다운로드1
12468정성태12/30/202010241Windows: 186. CMD Shell의 "Defaults"와 "Properties"에서 폰트 정보가 다른 문제 [1]
12467정성태12/29/202010151.NET Framework: 989. HttpContextAccessor를 통해 이해하는 AsyncLocal<T> [1]파일 다운로드1
12466정성태12/29/20208168.NET Framework: 988. C# - 지연 실행이 꼭 필요한 상황이 아니라면 singleton 패턴에서 DCLP보다는 static 초기화를 권장파일 다운로드1
12465정성태12/29/202011283.NET Framework: 987. .NET Profiler - FunctionID와 연관된 ClassID를 구할 수 없는 문제
12464정성태12/29/202010129.NET Framework: 986. pptfont.exe - PPT 파일에 숨겨진 폰트 설정을 일괄 삭제
12463정성태12/29/20209181개발 환경 구성: 520. RDP(mstsc.exe)의 다중 모니터 옵션 /multimon, /span
12462정성태12/27/202010783디버깅 기술: 177. windbg - (ASP.NET 환경에서 유용한) netext 확장
12461정성태12/21/202011592.NET Framework: 985. .NET 코드 리뷰 팁 [3]
12460정성태12/18/20209288기타: 78. 도서 소개 - C#으로 배우는 암호학
12459정성태12/16/20209712Linux: 35. C# - 리눅스 환경에서 클라이언트 소켓의 ephemeral port 재사용파일 다운로드1
12458정성태12/16/20209222오류 유형: 694. C# - Task.Start 메서드 호출 시 "System.InvalidOperationException: 'Start may not be called on a task that has completed.'" 예외 발생 [1]
12457정성태12/15/20208780Windows: 185. C# - Windows 10/2019부터 추가된 SIO_TCP_INFO파일 다운로드1
12456정성태12/15/20209042VS.NET IDE: 156. Visual Studio - "Migrate packages.config to PackageReference"
12455정성태12/15/20208547오류 유형: 693. DLL 로딩 시 0x800704ec - This Program is Blocked by Group Policy
12454정성태12/15/20209134Windows: 184. Windows - AppLocker의 "DLL Rules"를 이용해 임의 경로에 설치한 DLL의 로딩을 막는 방법 [1]
12453정성태12/14/202010117.NET Framework: 984. C# - bool / BOOL / VARIANT_BOOL에 대한 Interop [1]파일 다운로드1
12452정성태12/14/202010283Windows: 183. 설정은 가능하지만 구할 수는 없는 TcpTimedWaitDelay 값
12451정성태12/14/20209471Windows: 182. WMI Namespace를 열거하고, 그 안에 정의된 클래스를 열거하는 방법 [5]
12450정성태12/13/202010182.NET Framework: 983. C# - TIME_WAIT과 ephemeral port 재사용파일 다운로드1
12449정성태12/11/202010595.NET Framework: 982. C# - HttpClient에서의 ephemeral port 재사용 [2]파일 다운로드1
12448정성태12/11/202012213.NET Framework: 981. C# - HttpWebRequest, WebClient와 ephemeral port 재사용파일 다운로드1
12447정성태12/10/202010352.NET Framework: 980. C# - CopyFileEx API 사용 예제 코드파일 다운로드1
12446정성태12/10/202010962.NET Framework: 979. C# - CoCreateInstanceEx 사용 예제 코드파일 다운로드1
12445정성태12/8/20208428오류 유형: 692. C# Marshal.PtrToStructure - The structure must not be a value class.파일 다운로드1
... [46]  47  48  49  50  51  52  53  54  55  56  57  58  59  60  ...