SFTP,第一次听说,还以为是公司自己搞得一个东东呢,google了一下,原来是一种FTP协议,是可信任的FTP,类似于HTTPS协议。
这次项目就是要将一些数据文件打包通过SFTP传到德国的Server,所以中途是需要加密传输的,即通过SFTP进行数据的上传动作。
找了一个开源的东东,PSFTP,是一个绿色EXE档,C#中控制也很方便,于是自己封装了一下方便自己的应用。
PSFTP的命令是典型的Unix命令,很简单易懂 ,下面是其基本的命令:
C#中使用Process调用该EXE实现FTP的上传,参数如下:
C#中调用方式如下:
Upload#region Upload
/**////
/// Upload the files
///
/// output of the plugin during its running in console
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
/**////
/// Create Batch Script File
///
/// file full path
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
/**////
/// Upload the files
///
///
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
/**////
/// Create Batch Script File
///
///
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
关键字词: