C# 使用 Cmd.exe 获取 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 命令来执行这个文件。最后,它将命令行输出作为字符串输出到控制台。
原文地址: https://www.cveoy.top/t/topic/osZY 著作权归作者所有。请勿转载和采集!