Example #1
0
        private bool DownloadFile(string local_path_folder, string remote_path_file, string local_file_name = null, string remote_filename = null, bool bLog = true)
        {
            //LinuxTreeViewItem.ReconnectServer();
            //LinuxTreeViewItem.ReConnect();
            if (!IsConnected)
            {
                return(false);
            }

            try
            {
                string local;
                if (local_file_name != null)
                {
                    local = local_path_folder + "\\" + local_file_name;
                }
                else
                {
                    if (remote_filename == null)
                    {
                        string[] split = remote_path_file.Split('/');
                        remote_filename = split[split.Length - 1];
                    }
                    local = local_path_folder + "\\" + remote_filename;
                }

                if (!FileContoller.CreateDirectory(local_path_folder))
                {
                    return(false);
                }

                if (sftp.Exists(remote_path_file))
                {
                    FileStream fs = new FileStream(local, FileMode.Create);
                    sftp.DownloadFile(remote_path_file, fs);
                    Log.PrintLog(remote_path_file + " => " + local, "Classes.SSHManager.DownloadFile");
                    fs.Close();
                }
                else
                {
                    Log.PrintLog("Not found file " + remote_path_file, "Classes.SSHManager.DownloadFile");
                    return(false);
                }
            }
            catch (Exception e)
            {
                if (bLog)
                {
                    Log.ErrorIntoUI(e.Message, "downloadFile", Status.current.richTextBox_status);
                }
                Log.PrintError(e.Message, "Classes.SSHManager.DownloadFile");
                return(false);
            }
            return(true);
        }
Example #2
0
        private bool DownloadDirectory(string local_folder_path, string remote_directory_path, Regex filter_file = null, Regex filter_except_dir = null)
        {
            //LinuxTreeViewItem.ReconnectServer();
            //LinuxTreeViewItem.ReConnect();
            if (!IsConnected)
            {
                return(false);
            }

            try
            {
                if (!FileContoller.CreateDirectory(local_folder_path))
                {
                    return(false);
                }

                SftpFile[] files = PullListInDirectory(remote_directory_path);
                if (files == null)
                {
                    return(false);
                }
                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.ErrorIntoUI(e.Message, "DownloadDirectory", Status.current.richTextBox_status);
                Log.PrintError(e.Message, "Classes.SSHManager.DownloadDirectory");
                return(false);
            }
            return(true);
        }
Example #3
0
        public static bool Write(string path, string str)
        {
            if (path == null || str == null)
            {
                return(false);
            }
            try
            {
                // 경로에 디렉토리가 없으면 생성
                string dir = path;
                if (path[path.Length - 1] != '\\')
                {
                    dir = path.Substring(0, path.LastIndexOf('\\') + 1);
                }
                if (!FileContoller.CreateDirectory(dir))
                {
                    return(false);
                }

                // 경로에 파일 쓰기.
                FileStream fs = new FileStream(path, FileMode.Create);

                byte[] buffer /* = new byte[MAX_BUFFER]*/;
                int    size_write = 0;

                buffer     = Encoding.UTF8.GetBytes(str);
                size_write = buffer.Length;

                fs.Write(buffer, 0, size_write);

                fs.Close();
                return(true);
            }
            catch (Exception e)
            {
                Log.PrintError(e.Message + "<path = " + path + ">", "Classes.FileContoller.Write");
            }
            return(false);
        }