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

披着虎皮 的石头 2009-11-24 02:15:46
利用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
...全文
430 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
批执行文件的内容是:
cd /upload/ltest
get aaa.txt
quit

在命令行里,用 get aaa.txt表示下载到psftp.exe所在的目录。
而用c#程序时,不能这样,似乎是不认识local的目录,
必须指定下载的本地路径。即下面的写法,才是最完整正确的:
cd /upload/ltest
get aaa.txt c:\putty\aaa.txt
quit

经验教训:用类似此类开源的程序,操作设置上有简化版和完整版。
简化版用于图型化操作,完整版用于编程实现。

  • 打赏
  • 举报
回复
批执行文件的内容是:
cd /upload/ltest
get aaa.txt
quit
conan19771130 2009-11-24
  • 打赏
  • 举报
回复
mark
  • 打赏
  • 举报
回复
有没有其他简单的方法,C#实现windows访问linux的文件呢?

110,549

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧