【发布时间】:2017-05-05 18:18:45
【问题描述】:
我一直在尝试通过 C# 代码传输文件,但它似乎不起作用。尽管命令提示符中的以下行可以正常工作。
"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"/>
当发布请求时,我的 Http 代码是 200,但文件没有移动到 linux 服务器。我不确定这里出了什么问题。
【问题讨论】:
-
你为什么用 putty 而不是SSH.NET?
-
p.ExitCode的值是多少?output的值是多少? -
@JacobKrall 当我通过 IIS Express 在 localhost:49595 以调试模式运行代码时,该代码似乎可以工作。但是一旦我将它发布到网站上,它就不起作用了。
-
这没有回答问题!
-
@JacobKrall p.Exitcode 为 1,p.ouput 为空白。