C#如何使用FTP上传文件Class
C# How to use FTP upload File Class
对于这个愚蠢的问题提前致歉。我有一些关于在 C# 上使用 FTP 上传文件的问题,我需要指导:
public void UploadFile(string FullPathFilename)
{
string filename = Path.GetFileName(FullPathFilename);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(_remoteHost + filename);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(_remoteUser, _remotePass);
StreamReader sourceStream = new StreamReader(FullPathFilename);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
requestStream.Close();
sourceStream.Close();
}
所以,我不需要创建新文件夹,我用什么代替 [FtpWebRequest request = (FtpWebRequest)WebRequest.Create(_remoteHost + filename);]?这是在远程 ftp 站点中创建文件夹吗?
我在 FTP class 中使用此代码,并且我已经创建并填充了文件。我如何在我的 Program.cs 中调用它来将文件上传到 FTP 目录?
URL 的格式为:
WebRequest.Create("ftp://ftp.example.com/path/to/directory/filename.ext");
对于这个愚蠢的问题提前致歉。我有一些关于在 C# 上使用 FTP 上传文件的问题,我需要指导:
public void UploadFile(string FullPathFilename)
{
string filename = Path.GetFileName(FullPathFilename);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(_remoteHost + filename);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(_remoteUser, _remotePass);
StreamReader sourceStream = new StreamReader(FullPathFilename);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
requestStream.Close();
sourceStream.Close();
}
所以,我不需要创建新文件夹,我用什么代替 [FtpWebRequest request = (FtpWebRequest)WebRequest.Create(_remoteHost + filename);]?这是在远程 ftp 站点中创建文件夹吗? 我在 FTP class 中使用此代码,并且我已经创建并填充了文件。我如何在我的 Program.cs 中调用它来将文件上传到 FTP 目录?
URL 的格式为:
WebRequest.Create("ftp://ftp.example.com/path/to/directory/filename.ext");