簡體   English   中英

使用 C# 將文件傳輸到另一台 PC

[英]Transfer file to another PC using C#

我必須將文件傳輸到另一台已創建shared文件夾的 PC。 由於另一台 PC 是通過LAN電纜連接的,因此我能夠訪問此共享文件夾。

現在我想使用 C# 將文件傳輸到該shared文件夾。 我正在關注此鏈接中的教程。

代碼 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 服務器。 我已經用它的password創建了一個User 我在可以共享的目錄中添加了shared文件夾。 現在,我收到一個錯誤

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

對於行ftpStream = ftpRequest.GetRequestStream();

好吧,鑒於它是一個共享文件夾,只需使用File.Copy 對於這個簡單的任務,FTP 是一種矯枉過正的方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM