본문 바로가기

Web_Application/C#

[winform][c#] console 창 안보이게 node.js 실행하기



        private string NodeSqlFormatter(string sourceText)
        {
            Process p = new Process();
            DirectoryInfo startUpPath = new DirectoryInfo(Application.StartupPath);

            string[] delimiter = { "\\{{경로구분자}}" };          //경로 구분자
            string path = startUpPath.ToString();

            string[] paths = path.Split(delimiter, System.StringSplitOptions.RemoveEmptyEntries);
            string filePath = string.Format(@"{0}{1}", paths[0], delimiter[0]);

            string s;

            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.StandardOutputEncoding = Encoding.UTF8;      //한글 깨지면 encoding 변경
            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;                //console 창 안보이기
            p.StartInfo.CreateNoWindow = true;                                                    //console 창 안보이기
            p.StartInfo.FileName = string.Format("C:\\Program Files\\nodejs\\node.exe");            //노드 실행 파일 경로
            p.StartInfo.Arguments = string.Format("{0}\\{{js 파일명}}  \"{1}\"", filePath, textBox1.Text);
            p.Start();

            StreamReader reader = p.StandardOutput;
            StreamWriter writer = p.StandardInput;
            writer.AutoFlush = true;
            s = reader.ReadToEnd();
            s = s.Replace("\n", "\r\n");

            return s;
        }