Exemplo n.º 1
0
 /// <summary>
 /// 取得檔案最後修改日期
 /// </summary>
 /// <param name="ftpFolderPath">資料夾路徑,根目錄請代空字串</param>
 /// <param name="fileName">檔案完整名稱(含副檔名)</param>
 /// <returns></returns>
 public DateTime GetFileModifiedDate(string ftpFolderPath, string fileName)
 {
     try
     {
         if (IsFileExists(ftpFolderPath, fileName))
         {
             return(_sftp.GetLastAccessTime(string.Format("/{0}/{1}", ftpFolderPath, fileName)));
         }
         else
         {
             throw new Exception(string.Format("取得SFTP檔案修改日其失敗,原因:{0}", "SFTP上無此檔案"));
         }
     }
     catch (Exception ex)
     {
         throw new Exception(string.Format("取得SFTP檔案修改日其失敗,原因:{0}", ex.ToString()));
     }
 }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
 public void GetLastAccessTimeTest()
 {
     ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
     SftpClient target = new SftpClient(connectionInfo); // TODO: Initialize to an appropriate value
     string path = string.Empty; // TODO: Initialize to an appropriate value
     DateTime expected = new DateTime(); // TODO: Initialize to an appropriate value
     DateTime actual;
     actual = target.GetLastAccessTime(path);
     Assert.AreEqual(expected, actual);
     Assert.Inconclusive("Verify the correctness of this test method.");
 }
Exemplo n.º 4
0
        private string UploadFile(string local_path, string remote_directory, string remote_backup_dir = null)
        {
            //LinuxTreeViewItem.ReconnectServer();
            //LinuxTreeViewItem.ReConnect();
            if (!IsConnected)
            {
                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 (CreateRemoteDirectory(remote_directory))
                    {
                        if (remote_backup_dir != null && sftp.Exists(remote_file_path))
                        {
                            if (CreateRemoteDirectory(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";
                                string com = @"cp '" + remote_file_path + "' '" + remote_backup_file + "'";
                                ssh.RunCommand(com);
                                //SendCommand(com);
                            }
                            else
                            {
                                //fs.Close();
                                Log.PrintError("Create Directory Error", "Classes.SSHManager.UploadFile");
                                return(null);
                            }
                        }

                        //sftp.UploadFile(fs, remote_file_path, true);
                        string str  = FileContoller.Read(local_path);
                        string str1 = "echo \"" + str.Replace("\r", "").Replace("\\", "\\\\").Replace("\"", "\\\"").Replace("$", "\\$") + "\" > \"" + remote_file_path + "\"";
                        ssh.RunCommand(str1);
                        //SendCommand(str1);

                        Log.PrintLog(fi.Name + " => " + remote_file_path, "Classes.SSHManager.UploadFile");
                    }
                    else
                    {
                        remote_file_path = null;
                    }
                    //fs.Close();
                }
                else
                {
                    Log.ErrorIntoUI("Not Exist File [" + local_path + "]", "upload file", Status.current.richTextBox_status);
                    Log.PrintError("Not Exist File [" + local_path + "]", "Classes.SSHManager.UploadFile");
                    return(null);
                }
            }
            catch (Exception e)
            {
                Log.ErrorIntoUI(e.Message + "/ " + local_path + " -> " + remote_directory, "UploadFile", Status.current.richTextBox_status);
                Log.PrintError(e.Message + "/ " + local_path + " -> " + remote_directory, "Classes.SSHManager.UploadFile");
                return(null);
            }
            return(remote_file_path);
        }