I have been trying to transfer files through a C# code, but it doesn't seem to work. Although the following line in command prompt works fine.
"C:\Program Files (x86)\PuTTY\pscp.exe" -l user -pw password C:\Users\user1\Desktop\\transfer1.txt 000.000.00.000:/home/doc
The communication class is
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";
}}}
and the SendFilesController
looks like
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");
}}}
And in app.config
, I have
<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"/>
When a post request is made, the HTTP code is 200 but the file is not moving to the Linux server. I am not sure what is going wrong here.
The code seems to work when I am running it in debug mode via IIS Express at localhost:49595. But is is not working once I publish it to a website. p.Exitcode
is 1 and p.ouput
is blank.