使用C将文件传输到另一台PC#
我必须将文件传输到另一台我已创建了使用C将文件传输到另一台PC#,c#,ftp,C#,Ftp,我必须将文件传输到另一台我已创建了共享文件夹的PC。我可以访问此共享文件夹,因为另一台电脑是通过局域网电缆连接的 现在我想用C#将一个文件传输到共享的文件夹。我正在跟随一个来自的教程 代码MWE: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace FileTransferApplication { class ftp {
共享文件夹的PC。我可以访问此共享文件夹,因为另一台电脑是通过局域网电缆连接的
现在我想用C#将一个文件传输到共享的文件夹。我正在跟随一个来自的教程
代码MWE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FileTransferApplication
{
class ftp
{
private string host = null;
private string user = null;
private string pass = null;
private System.Net.FtpWebRequest ftpRequest = null;
private System.Net.FtpWebResponse ftpResponse = null;
private System.IO.Stream ftpStream = null;
private int bufferSize = 2048;
static void Main()
{
ftp ftpClient = new ftp(@"ftp://X.X.0.20/", "dst username", "dst pc password");
/* Upload a File */
ftpClient.upload(@"shared\test.txt", "E:/SampleFile2.txt");
ftpClient = null;
}
/* Construct Object */
public ftp(string hostIP, string userName, string password) { host = hostIP; user = userName; pass = password; }
/* Upload File */
public void upload(string remoteFile, string localFile)
{
try
{
/* Create an FTP Request */
ftpRequest = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(host + "/" + remoteFile);
/* Log in to the FTP Server with the User Name and Password Provided */
ftpRequest.Credentials = new System.Net.NetworkCredential(user, pass);
/* When in doubt, use these options */
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
/* Specify the Type of FTP Request */
ftpRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
/* Establish Return Communication with the FTP Server */
ftpStream = ftpRequest.GetRequestStream();
/* Open a File Stream to Read the File for Upload */
System.IO.FileStream localFileStream = new System.IO.FileStream(localFile, System.IO.FileMode.Open);
/* Buffer for the Downloaded Data */
byte[] byteBuffer = new byte[bufferSize];
int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
/* Upload the File by Sending the Buffered Data Until the Transfer is Complete */
try
{
while (bytesSent != 0)
{
ftpStream.Write(byteBuffer, 0, bytesSent);
bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
}
}
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
/* Resource Cleanup */
localFileStream.Close();
ftpStream.Close();
ftpRequest = null;
}
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
return;
}
}
}
PS:我正在使用Windows-7
UPDATE-1:实际上,由于我仍处于传输文件的学习阶段,因此我已出于测试目的共享了该文件夹。但我的实际需求是使用IP地址、用户名和密码将文件传输到服务器/PC
问题:我正在我的第二台电脑上运行一个Filezilla ftp服务器。我已经创建了一个用户及其密码。我已经在可以共享的目录中添加了shared
文件夹。现在,我得到一个错误
System.Net.WebException: The remote server returned an error: (530) Not logged in.
对于行ftpStream=ftpRequest.GetRequestStream()代码>好吧,既然它是一个共享文件夹,只需使用。FTP对于这个简单的任务来说是一种过火的方式。如果不能重新创建问题,我看不出这是如何解决的。您解决此问题的最佳位置不是usis,而是目标PC启用ftp?“IP地址、用户名和密码”非常模糊。为此,您必须在计算机上安装一些文件传输服务器(FTP、SFTP或其他)。是吗?是的,我正在目标电脑上运行Filezila服务器。请看一下我更新的问题@codecast:对不起,我不明白代码为什么不是MWE。上面的代码编译时没有任何错误。我已经写了《代码》ftp://X.X.0.20/“,”dst用户名“,”dst pc密码“
在这里的代码中,但在实际代码中,我传递的是正确的IP、用户名和密码。请让我知道我还应该在问题中添加什么。事实上,由于我仍处于传输文件的学习阶段,我已经出于测试目的共享了该文件夹。但我的实际需求是使用IP地址、用户名和密码将文件传输到服务器/PC。