使用C#将文件传输到另一台PC

时间:2017-02-08 11:51:13

标签: c# ftp

我必须将文件传输到我创建了shared文件夹的另一台PC。我可以访问此共享文件夹,因为另一台PC通过 LAN 电缆连接。

现在我想使用C#将文件传输到shared文件夹。我正在关注this link的教程。

代码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。

问题:我正在第二台PC上运行Filezilla ftp服务器。我已经使用User创建了password alogn。我在可以共享的目录中添加了shared文件夹。现在,我收到了一个错误

System.Net.WebException: The remote server returned an error: (530) Not logged in.    

代码行ftpStream = ftpRequest.GetRequestStream();

1 个答案:

答案 0 :(得分:1)

好吧,鉴于它是一个共享文件夹,只需使用File.Copy即可。 FTP对于这个简单的任务来说太过分了。