shared
フォルダを作成した別のPCにファイルを転送する必要があります。別のPCがLANケーブルで接続されているので、この共有フォルダにアクセスできます。Cを使用して別のPCにファイルを転送する
これで、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にファイルを転送することです。
質問:私は2台目のPCでFilezillaのFTPサーバーを使用しています。私は既にUser
alognをpassword
で作成しました。共有できるディレクトリにshared
フォルダを追加しました。今、私はそれだけでFile.Copy
を使用し、共有フォルダのことを考えると、ラインftpStream = ftpRequest.GetRequestStream();
問題を再作成することができず、これはどのように答えることができません。あなたのベストは、この問題を解決するために、私たちではない – Liam
は、宛先PCのFTPを有効にする? –
* "IPアドレス、ユーザー名、パスワード" *はかなり曖昧です。そのためには、マシンにファイル転送サーバー(FTP、SFTPなど)がインストールされている必要があります。あなたは? –