在C#里怎么实现:windows访问(读取)linux上的文件?

披着虎皮 的石头 披着虎皮 的石头 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
利用psftp.exe可以在dos界面下,实现linux文件的上传和下载。
还可以通过调用Process的命令方式,实现上传文件到linux上,
但是实现文件下载到windows时,就不行。下面是下载时的源代码
而同样的upload时,是可以的,不知道为什么会这样:

#region Download

// Upload the files
// output of the plugin during its running in console
public string Download()
   {
   string outPutMessage = "";
   string scriptLocation = "";

//Create Script File
   scriptLocation = this.CreateScriptFile();
  
   //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;

   //Create new Process
   Process process = new Process();
   try
   {
   process.StartInfo = processInfo;
   Boolean b = 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.StandardError.ReadToEnd();

   //Wait for the process exit
   process.WaitForExit();

   //Close all the modules opened
   process.Close();
   process.Dispose();

   //Delete the script file OK
   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

   #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(" get " + this.m_DownloadFiles + Environment.NewLine);
//}

//Close the session
sbdScript.Append(" quit ");

//Save the Script to templocation
scriptLocation = this.m_TempLocation + @"" + System.Guid.NewGuid().ToString() + ".bat";

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
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
披着虎皮 的石头
C# 111,128 成员 642,537 内容