private static bool sendCommand(string command)
        {
            //LinuxTreeViewItem.ReconnectServer();
            //LinuxTreeViewItem.ReConnect();
            if (!SSHController.ReConnect(timeout_connect_ms))
            {
                return(false);
            }

            try
            {
                // send
                if (shell_stream_reader != null)
                {
                    shell_stream_writer.Write(command);
                    shell_stream_writer.Write("\n");
                    shell_stream_writer.Flush();
                    Log.PrintConsole(command, "send command" /*, test4.m_wnd.richTextBox_status*/);
                    return(true);
                }
            }
            catch (Exception ex)
            {
                Log.PrintError(ex.Message, "send command", Status.current.richTextBox_status);
            }
            return(false);
        }
        public static void MakeTree(SftpFileTree cur, string remote_path_dir)
        {
            cur.children.Clear();

            SftpFile[] files = SSHController.PullListInDirectory(remote_path_dir);
            for (int i = 0; i < files.Length; i++)
            {
                if (files[i].Name == ".")
                {
                    continue;
                }

                SftpFileTree child = new SftpFileTree(files[i]);

                cur.children.Add(child);
                child.parent = cur;

                if (files[i].IsDirectory)
                {
                    if (files[i].Name == "..")
                    {
                        if (cur.parent != null)
                        {
                            child.children = cur.parent.children;
                        }
                    }
                    else
                    {
                        MakeTree(child, files[i].FullName);
                    }
                }
            }
        }
        public static SftpFile[] PullListInDirectory(string Path)
        {
            if (!SSHController.ReConnect(timeout_connect_ms))
            {
                return(null);
            }
            if (Path == null)
            {
                return(null);
            }

            return(_PullListInDirectory(Path));
        }
        private static string LoadEnvCoHome()
        {
            if (!SSHController.sendCommand(cmd_get_co_home))
            {
                return(null);
            }
            string env_co_home = readCoHomeBlocking(cmd_get_co_home);

            if (env_co_home == null || env_co_home == "")
            {
                Log.PrintError("not defined $CO_HOME\r", "load $CO_HOME", Status.current.richTextBox_status);
                return(null);
            }
            Log.PrintConsole("$CO_HOME = " + env_co_home, "load $CO_HOME");
            return(env_co_home);
        }
        private static bool downloadDirectory(string local_folder_path, string remote_directory_path, Regex filter_file = null, Regex filter_except_dir = null)
        {
            //LinuxTreeViewItem.ReconnectServer();
            //LinuxTreeViewItem.ReConnect();
            if (!SSHController.ReConnect(timeout_connect_ms))
            {
                return(false);
            }

            try
            {
                FileContoller.CreateDirectory(local_folder_path);

                SftpFile[] files = PullListInDirectory(remote_directory_path);
                for (int i = 0; i < files.Length; i++)
                {
                    if (files[i].Name == "." || files[i].Name == "..")
                    {
                        continue;
                    }

                    if (files[i].IsDirectory &&
                        (filter_except_dir == null || !filter_except_dir.IsMatch(files[i].Name)))
                    {
                        string re_local_folder_path = local_folder_path + files[i].Name + @"\";
                        downloadDirectory(re_local_folder_path, files[i].FullName, filter_file, filter_except_dir);
                        continue;
                    }

                    if (filter_file != null && !filter_file.IsMatch(files[i].Name))
                    {
                        continue;
                    }

                    downloadFile(local_folder_path, files[i].FullName, files[i].Name, files[i].Name);
                }
            }
            catch (Exception e)
            {
                Log.PrintError(e.Message, "downloadFile", Status.current.richTextBox_status);
                return(false);
            }
            return(true);
        }
        public static bool downloadFile(string local_path_folder, string remote_path_file, string local_file_name = null, string remote_filename = null)
        {
            //LinuxTreeViewItem.ReconnectServer();
            //LinuxTreeViewItem.ReConnect();
            if (!SSHController.ReConnect(timeout_connect_ms))
            {
                return(false);
            }

            try
            {
                if (remote_filename == null)
                {
                    string[] split = remote_path_file.Split('/');
                    remote_filename = split[split.Length - 1];
                }

                string local;
                if (local_file_name != null)
                {
                    local = local_path_folder + local_file_name;
                }
                else
                {
                    local = local_path_folder + remote_filename;
                }

                FileContoller.CreateDirectory(local_path_folder);

                FileStream fs = new FileStream(local, FileMode.Create);
                sftp.DownloadFile(remote_path_file, fs);
                Log.PrintConsole(remote_path_file + " => " + local, "downloadFile" /*, test4.m_wnd.richTextBox_status*/);
                fs.Close();
            }
            catch (Exception e)
            {
                Log.PrintError(e.Message, "downloadFile", Status.current.richTextBox_status);
                return(false);
            }
            return(true);
        }
        public static string UploadFile(string local_path, string remote_directory, string remote_backup_dir = null)
        {
            //LinuxTreeViewItem.ReconnectServer();
            //LinuxTreeViewItem.ReConnect();
            if (!SSHController.ReConnect(timeout_connect_ms))
            {
                return(null);
            }


            string remote_file_path = null;

            try
            {
                FileInfo fi = new FileInfo(local_path);
                if (fi.Exists)
                {
                    FileStream fs = File.Open(local_path, FileMode.Open, FileAccess.Read);
                    remote_file_path = remote_directory + fi.Name;
                    //if(isOverride)
                    //{
                    //	sftp.UploadFile(fs, remote_file_path);
                    //	Log.PrintConsole(fi.Name + " => " + remote_file_path, "upload file"/*, test4.m_wnd.richTextBox_status*/);
                    //}
                    if (remote_backup_dir != null && sftp.Exists(remote_file_path))
                    {
                        if (CreateDirectory(remote_backup_dir))
                        {
                            DateTime dt;
                            //dt = DateTime.Now;

                            // 원래는 서버시간으로 생성해야함.
                            // 서버마다 시간을 알수있는 함수가 다를수 있으므로 sftp를 사용
                            // 위 if 문의 sftp.Exists(remote_file_path) 에서 엑세스한 시간을 가져옴.
                            dt = sftp.GetLastAccessTime(remote_file_path);

                            // '파일 명'.'연도'.'달'.'날짜'.'시간'.'분'.'초'.backup 형식으로 백업파일 생성
                            string remote_backup_file = remote_backup_dir + fi.Name + dt.ToString(".yyyy.MM.dd.hh.mm.ss") + ".backup";
                            ssh.RunCommand(@"cp " + remote_file_path + " " + remote_backup_file);
                        }
                        else
                        {
                            fs.Close();
                            Log.PrintError("Create Directory Error", "upload file");
                            return(null);
                        }
                    }
                    if (CreateDirectory(remote_directory))
                    {
                        sftp.UploadFile(fs, remote_file_path, true);
                        Log.PrintConsole(fi.Name + " => " + remote_file_path, "upload file" /*, test4.m_wnd.richTextBox_status*/);
                    }
                    fs.Close();
                }
                else
                {
                    Log.PrintError("Not Exist File", "upload file", Status.current.richTextBox_status);
                    return(null);
                }
            }
            catch (Exception e)
            {
                Log.PrintError(e.Message, "UploadFile", Status.current.richTextBox_status);
                return(null);
            }
            return(remote_file_path);
        }