国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

C#中應用PSFTP實現SFTP上傳

2024-07-21 02:26:58
字體:
來源:轉載
供稿:網友

sftp,第一次聽說,還以為是公司自己搞得一個東東呢,google了一下,原來是一種ftp協(xié)議,是可信任的ftp,類似于https協(xié)議。

這次項目就是要將一些數據文件打包通過sftp傳到德國的server,所以中途是需要加密傳輸的,即通過sftp進行數據的上傳動作。

找了一個開源的東東,psftp,是一個綠色exe檔,c#中控制也很方便,于是自己封裝了一下方便自己的應用。

psftp的命令是典型的unix命令,很簡單易懂 ,下面是其基本的命令:

c#中使用process調用該exe實現ftp的上傳,參數如下:

c#中調用方式如下:

 

upload#region upload
/**//// <summary>
/// upload the files
/// </summary>
/// <returns>output of the plugin during its running in console</returns>

public string upload()
...{
    
string outputmessage = "";
    
string scriptlocation = "";

    
//create script file
    scriptlocation = this.createscriptfile();

    
begin for processstartinfo#region begin for processstartinfo
    
//run the upload event
    processstartinfo processinfo = new processstartinfo();
    
//set the shell command(the plugins' path)
    processinfo.filename = this.m_shellcommand;
    
//don't show console window
    processinfo.createnowindow = true;
    
//don't use shell to execute this script
    processinfo.useshellexecute = false;      
    
//open process error output
    processinfo.redirectstandarderror = true
    
//open process input
    processinfo.redirectstandardinput = true
    
//open process output
    processinfo.redirectstandardoutput = true;
    
//get process arguments
    string arguments = "";
    arguments 
+= this.m_userid + "@" + this.m_servername + " "//login server with specified userid
    arguments += "-pw " + this.m_password + " "//login with specified password
    arguments += "-p " + this.m_port + " "//connect to specified port
    arguments += "-b " + scriptlocation + " "//use specified batchfile
    arguments += "-be"//don't stop batchfile processing if errors
    processinfo.arguments = arguments;
    
#endregion

    
//create new process
    process process = new process();
    
try
    
...{
        
//start execute the processstartinfo
        process.startinfo = processinfo;
        
//run the process
        process.start();
        
//input "y" for the psftp's first login prompt
        
//just for the security information
        process.standardinput.writeline("y");

        
//get the return message from process(error and output information)
        
//this message will be logged to file for debug!
        outputmessage += process.standardoutput.readtoend();
        outputmessage 
+= process.standarderror.readtoend();
        
//wait for the process exit
        process.waitforexit();
        
//close all the modules opened
        process.close();
        process.dispose();
        
//delete the script file
        file.delete(scriptlocation);
        
return outputmessage;
    }

    
catch(exception ex)
    
...{
        process.dispose();
        
//delete the script file
        file.delete(scriptlocation);
        
throw new exception("error occured during upload file to remote server!",ex);
    }

}

#endregion


createscriptfile#region createscriptfile
/**//// <summary>
/// create batch script file
/// </summary>
/// <returns>file full path</returns>

private string createscriptfile()
...{
    streamwriter filestream;
    
string scriptlocation = "";
    
//get the batch script to execute
    stringbuilder sbdscript = new stringbuilder();
    
//redirect to the default remote location
    sbdscript.append("cd " + this.m_remotelocation + environment.newline);
    
//upload files
    foreach(object file in this.m_uploadfiles)
    
...{
        sbdscript.append(
"put " + (string)file + environment.newline);
    }


    
//close the session
    sbdscript.append("quit");
    
//save the script to templocation
    scriptlocation = this.m_templocation + @"" + system.guid.newguid().tostring() + ".scr";

    
try
    
...{                
        filestream 
= new streamwriter(scriptlocation);
        filestream.write(sbdscript.tostring());
        filestream.close();
    }

    
catch(exception ex)
    
...{
        filestream 
= null;
        
throw new exception("error occured during create script file!",ex);
    }

    
return scriptlocation;
}

#endregion
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 昔阳县| 临城县| 汤阴县| 玉山县| 新宾| 柘城县| 平陆县| 浠水县| 乐都县| 鄢陵县| 额尔古纳市| 元朗区| 镇巴县| 凤翔县| 阳信县| 株洲市| 安图县| 特克斯县| 扬州市| 神池县| 乳山市| 武川县| 兴文县| 千阳县| 高台县| 新乡县| 南澳县| 长阳| 柞水县| 阿图什市| 贡觉县| 安丘市| 来安县| 昆山市| 通辽市| 上林县| 梅河口市| 万宁市| 图木舒克市| 桐城市| 高清|