我正在使用上传文件到ftp FtpWebRequest.我需要显示已完成的状态.
到目前为止我的代码是:
public void Upload(string filename, string url)
{
FileInfo fileInf = new FileInfo(filename);
string uri = "ftp://" + url + "/" + fileInf.Name;
FtpWebRequest reqFTP;
//string uri = "ftp://" + Host + "/public_html/testing/blogtest/" + fileInf.Name;
// Create FtpWebRequest object from the Uri provided
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
// Provide the WebPermission Credintials
reqFTP.Credentials = new NetworkCredential(Username, Password);
// By default KeepAlive is true, where the control connection is not closed
// after a command is executed.
reqFTP.KeepAlive …Run Code Online (Sandbox Code Playgroud) 一般信息
我还在学习C#.为了帮助自己,我正在尝试创建一个程序,它将自动将我的所有本地项目与我的FTP服务器上的文件夹同步.这样,无论我是在学校还是在家,我总是可以使用相同的项目.
我知道像Dropbox这样的程序已经为我做了这个,但我想创造类似的东西,我自己会教我很多东西.
问题
我迈向目标的第一步是从我的FTP服务器下载所有文件,子目录和子文件.我已经设法从下面的代码下载目录中的所有文件.但是,我的代码只列出了主目录中的文件夹名称和文件.子文件夹和子文件永远不会返回,也永远不会下载.除此之外,服务器返回550错误,因为我正在尝试下载文件夹,就像它们是文件一样.我已经在这上面了4个多小时了,但我找不到任何关于如何解决这些问题并让它发挥作用的事情.因此,我希望你们能帮助我:)
码
public string[] GetFileList()
{
string[] downloadFiles;
StringBuilder result = new StringBuilder();
WebResponse response = null;
StreamReader reader = null;
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);
request.UseBinary = true;
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = new NetworkCredential(ftpUserName, ftpPassWord);
request.KeepAlive = false;
request.UsePassive = false;
response = request.GetResponse();
reader = new StreamReader(response.GetResponseStream());
string line = reader.ReadLine();
while (line != null)
{
result.Append(line);
result.Append("\n");
line = reader.ReadLine();
}
result.Remove(result.ToString().LastIndexOf('\n'), 1);
return result.ToString().Split('\n');
}
catch (Exception ex)
{
if …Run Code Online (Sandbox Code Playgroud) 我需要一些指导.我需要在C#中开发一个可自定义的FTP,它应该使用App.Config文件进行配置.此外,FTP应该再次从任何客户端将数据推送到任何服务器依赖于配置文件.
如果有人可以指导,如果有任何API或任何其他有用的建议,或者让我朝着正确的方向前进,我将不胜感激.
我有一个问题,问题是我可以下载ftp文件但我下载的文件没有恢复设施和多部分文件下载,因为有超过500 MB文件的大文件我不能连续下载文件因为我断开连接并且它从头开始下载我希望我的代码是一个恢复工具,如果它断开连接
我正在使用的代码是
public string[] GetFileList()
{
string[] downloadFiles;
StringBuilder result = new StringBuilder();
FtpWebRequest reqFTP;
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/"));
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
WebResponse response = reqFTP.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
//MessageBox.Show(reader.ReadToEnd());
string line = reader.ReadLine();
while (line != null)
{
result.Append(line);
result.Append("\n");
line = reader.ReadLine();
}
result.Remove(result.ToString().LastIndexOf('\n'), 1);
reader.Close();
response.Close();
//MessageBox.Show(response.StatusDescription);
return result.ToString().Split('\n');
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
downloadFiles = null;
return downloadFiles; …Run Code Online (Sandbox Code Playgroud) 作为初级开发人员,我应该找到一个使用ftp下载文件的解决方案,我有这个代码.
它工作但有时,我无法打开下载的文件.
public static bool DownloadDocument(string ftpPath, string downloadPath) {
bool retVal = false;
try {
Uri serverUri = new Uri(ftpPath);
if (serverUri.Scheme != Uri.UriSchemeFtp) {
return false;
}
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(ftpPath);
reqFTP.Credentials = new NetworkCredential(Tools.FtpUserName, Tools.FtpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Proxy = null;
reqFTP.UsePassive = false;
using (FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse()) {
using (Stream responseStream = response.GetResponseStream()) {
using (FileStream writeStream = new FileStream(downloadPath, FileMode.Create)) {
int Length = 1024 * 1024 …Run Code Online (Sandbox Code Playgroud) 我需要从 C# 代码将文件夹(包含子文件夹和文件)从一台服务器上传到另一台服务器。我做了一些研究,发现我们可以使用 FTP 来实现这一点。但这样我就只能移动文件,而不能移动整个文件夹。感谢这里的任何帮助。