c# cmdexe 调用FTP获取文件列表
以下是使用C#调用cmd.exe获取FTP文件列表的示例代码:
using System;
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
string ftpUrl = "ftp://ftp.example.com"; // FTP服务器地址
string ftpUsername = "username"; // FTP登录用户名
string ftpPassword = "password"; // FTP登录密码
// 构建命令行参数
string cmdArgs = $"/c \"echo user {ftpUsername} {ftpPassword} > cmd.txt && echo dir >> cmd.txt && ftp -n -s:cmd.txt {ftpUrl}\"";
// 创建进程对象
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = cmdArgs;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
// 启动进程并等待结束
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
// 输出FTP文件列表
Console.WriteLine(output);
}
}
该示例代码使用了Windows命令行中的ftp命令来获取FTP文件列表。它通过创建一个cmd.txt文件来模拟用户在命令行中输入FTP命令,然后使用ftp -n -s:cmd.txt命令来执行这个文件。最后,它将命令行输出作为字符串输出到控制台
原文地址: http://www.cveoy.top/t/topic/f250 著作权归作者所有。请勿转载和采集!