使用具有特定参数的 C# 代码通过 Fluent FTP 下载文件

问题描述 投票:0回答:1

我有一个代码,我想打开系统默认程序文件中的文件,该文件放置在可通过 ftp 访问的远程服务器上,所以我需要帮助找到修复它的最佳解决方案。请尽快帮助我

FtpClient client = GetFtpClient();
 string remoteDirectory = client.GetWorkingDirectory();
 string remoteFileName = name;
 string localDirectory = Path.GetTempPath();
 string localFileName = Path.GetFileName(remoteFileName);
 string localFilePath = Path.Combine(localDirectory, localFileName);
 if (!Directory.Exists(localDirectory))
 {
     Directory.CreateDirectory(localDirectory);
 }
 client.Connect();
 try
 {                            
     var status = client.DownloadFile(localFilePath, Path.Combine(remoteDirectory, remoteFileName), FtpLocalExists.Overwrite, FtpVerify.Retry);
     switch (status)
     {
         case FtpStatus.Success:
             Console.WriteLine("File downloaded successfully.");
             Process.Start(localFilePath);
             MessageBox.Show("Selected item is a file.");
             break;
         case FtpStatus.Failed:
             MessageBox.Show("Error downloading file.");
             break;
         default:
             MessageBox.Show("Unknown error occurred.");
             break;
     }
 }
 catch (Exception ex)
 {
     MessageBox.Show("Error downloading file: " + ex.Message);
 }
 finally
 {
     client.Disconnect();
 }

我尝试通过多个 FTP API 下载文件的解决方案

c# ftp software-design file-manager
1个回答
0
投票

不完全确定这是否是您想要的,但要在默认程序中打开下载的文件(对于.net core)尝试

  using(Process p = new Process())
  {
    p.StartInfo = new ProcessStartInfo(Path.Combine(localFilePath,localFileName))
    {
      UseShellExecute = true
    };
    p.Start();
  }  
© www.soinside.com 2019 - 2024. All rights reserved.