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
新聞熱點
疑難解答