【发布时间】:2020-01-31 17:35:42
【问题描述】:
SshNet 使用 SFTP 上传一些文件,但是我在找到正确创建目录的方法时遇到了问题。我刚刚发现我创建了一些意大利面条代码,我想知道是否有人可以帮助我完成这项任务,我将不胜感激。
using Renci.SshNet;
using System;
using System.IO;
using System.Configuration;
namespace SFTPNamespace
{
class mySFTPClass
{
public static void CrazySFTP(string pathRemoteFile, string pathLocalFile, Action<ulong> downloadCallback = null)
{
string[] folders = Directory.GetFileSystemEntries(pathLocalFile, "*.", SearchOption.AllDirectories);
string[] txtfiles = Directory.GetFileSystemEntries(pathLocalFile, "*.txt", SearchOption.AllDirectories);
string[] entries = Directory.GetFileSystemEntries(pathLocalFile, "*.*", SearchOption.AllDirectories);
int totalCount_folders= folders.Length;
int totalCount_entries = entries.Length;
int totalCount_txtfiles = txtfiles.Length;
Console.Write("Found #"
+ totalCount_entries + " files which #"
+ totalCount_folders + " are dirs and #"
+ totalCount_txtfiles + " are txt files.\n" );
string myServer= ConfigurationManager.AppSettings["Server"];
int myPort= int.Parse(ConfigurationManager.AppSettings["Port"]);
string myUser= ConfigurationManager.AppSettings["User"];
string myPassword= ConfigurationManager.AppSettings["Password"];
Console.Write("Trying to connect to the server. \n");
try
{
using (SftpClient sftp = new SftpClient(myServer, myPort, myUser, myPassword))
{
sftp.Connect();
if (sftp.IsConnected)
{
Console.Write("Connection okay.\n");
foreach (string folder in folders)
{
string folderDirectory = Path.GetDirectoryName(folder);
folderDirectory = folderDirectory.Replace("C:", "").Replace("local", "").Replace("path", "").Replace("\\", "");
string folderName = Path.GetFileName(folder);
if (sftp.Exists("/remote/path/" + folderName))
{
Console.Write("Dir already exists.\n");
}
else
{
Console.Write("Creating directory in : /remote/path/" + folderDirectory + "/" + folderName + ".\n");
sftp.CreateDirectory("/remote/path/" + folderDirectory + "/" + folderName);
}
}
foreach (string txtfile in txtfiles)
{
string txtDirectory = Path.GetDirectoryName(txtfile);
txtDirectory = txtDirectory.Replace("C:\\local\\path\\", "").Replace("\\", "/");
string txtName = Path.GetFileName(txtfile);
if (sftp.Exists("/remote/path/" + txtDirectory + "/" + txtName))
{
Console.Write("File already exists.\n");
}
else
{
Console.Write("Writing files in : /remote/path/" + txtDirectory + "/" + txtName + "\n");
using (FileStream fileStream = new FileStream(txtfile, FileMode.Open))
{
sftp.BufferSize = 4 * 1024;
sftp.UploadFile(fileStream, "/remote/path/" + txtDirectory + "/" + txtfile, true, null);
}
}
}
Console.Write("Files uploaded successfully.\n");
Console.Write("Disconnecting from server.\n");
sftp.Disconnect();
}
else
{
Console.Write("Connection error.\n");
}
}
}
catch (Exception e)
{
Console.Write(e.Message + ".\n");
}
}
static void Main(string[] args)
{
CrazySFTP(@"/remote/path/", @"C:\local\path\");
Console.Write("Press a key to exit cli.\n");
Console.ReadLine();
}
}
}
-编辑- 好的,现在它可以工作了,但是它崩溃了,很多异常被抛出,我找不到理由我会深入研究这个,当我得到答案时会回来
【问题讨论】:
-
你为什么不使用另一个库而不是那个库? nuget 搜索中有很多这样的内容。
-
我被告知要使用这个,但我对 c# 有点陌生,你能推荐我一个更好/更简单的选择吗?
-
在项目资源管理器窗口中单击“引用”-“管理 nuget 包”。搜索“sftp”。选择任何具有大量下载和符合您目的的许可证的软件包。
-
刚刚解决了这个问题,不得不使用替换而不是进行大量拆分,现在它工作得很好,但是我在上传 0kbs 的文件时遇到了问题,我会在解决这个问题时发布我的答案跨度>