我一直在試圖通過C#代碼傳輸文件,但似乎並不奏效。儘管命令提示符中的以下行可以正常工作。在C#中使用pscp在windows和linux之間通過post請求進行文件傳輸
"C:\Program Files (x86)\PuTTY\pscp.exe" -l user -pw password C:\Users\user1\Desktop\\transfer1.txt 000.000.00.000:/home/doc
通信類
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
namespace Server.Controllers
{
public class ComLinux
{
public static string TransferFilesToSever(string args)
{
Process p = new Process();
p.StartInfo.FileName = @"C:\Program Files (x86)\PuTTY\pscp.exe";
p.StartInfo.Arguments = args;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = false;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
return "Done";
}}}
和SendFilesController看起來像
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace Server.Controllers
{
[Route("api/[controller]")]
public class SendFilesController : Controller
{
[HttpPost]
public IActionResult Post([FromBody] string cmdTostart)
{
var WindowsPath = ConfigurationManager.AppSettings["WindowsPath"].ToString();
var LinuxPath = ConfigurationManager.AppSettings["LinuxPath"].ToString();
var LinuxUserPass = ConfigurationManager.AppSettings["LinuxUserPass"].ToString();
var WindowcommandtoL = LinuxUserPass + " " + WindowsPath + "transfer1.txt" + " " + LinuxPath;
var resultw = ComLinux.TransferFilesToSever(WindowcommandtoL);
return Content("OK");
}}}
而在app.config中,我有
<add key="LinuxUserPass" value="-l user -pw password"/>
<add key="WindowsPath" value="C:\Users\user1\Desktop\"/>
<add key="LinuxPath" value="000.00.000://home/doc"/>
當POST請求製作,Ithe Http代碼是200但是該文件沒有移動到Linux服務器。我不確定這裏發生了什麼問題。
爲什麼使用putty代替[SSH.NET](https://sshnet.codeplex.com/)? – mason
'p.ExitCode'的值是什麼? 'output'的價值是什麼? –
@JacobKrall當我通過IIS Express在調試模式下在localhost:49595上運行代碼時,該代碼似乎工作正常。但是,一旦我將其發佈到網站上,它就無法正常工作。 – anse15