使用c#进行ftp登录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/541712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 07:27:40  来源:igfitidea点击:

ftp login using c#

c#ftp

提问by Freakingout

How do you Login to FTP using C#?

你如何使用 C# 登录到 FTP?

回答by bendewey

Here is a very nice FTP Client for C#

这是一个非常好的 C# FTP 客户端

http://www.codeproject.com/KB/IP/ftplibrary.aspx

http://www.codeproject.com/KB/IP/ftplibrary.aspx

Snippett from Link

来自链接的片段

//Get the basic FtpWebRequest object with the
//common settings and security
private FtpWebRequest GetRequest(string URI)
{
    //create request
    FtpWebRequest result = (FtpWebRequest)FtpWebRequest.Create(URI);
    //Set the login details
    result.Credentials = GetCredentials();
    //Do not keep alive (stateless mode)
    result.KeepAlive = false;
    return result;
}

/// <summary>
/// Get the credentials from username/password
/// </summary>
private System.Net.ICredentials GetCredentials()
{
    return new System.Net.NetworkCredential(Username, Password);
}

回答by Svish

Something like:

就像是:

var ftp = System.Net.FtpWebRequest(some url);
ftp.Credentials = something;

I think... :)

我认为... :)

回答by Ian Nelson

Native FTP support in .NET is fiddly.

.NET 中的本机 FTP 支持很繁琐。

I suggest using the free edtFTPnet component - I have used this in enterprisey applications with no problems whatsoever.

我建议使用免费的 edtFTPnet 组件 - 我已经在企业应用程序中使用了它,没有任何问题。

http://www.enterprisedt.com/products/edtftpnet/overview.html

http://www.enterprisedt.com/products/edtftpnet/overview.html

回答by Edgar

var url = "ftp://server";    
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(url);
request.Credentials = new NetworkCredential(username, password);

回答by danseery

I'm just throwing my hat in the ring for anyone searching this topic. Here is how I did this recently. I made an FTPProvider class to handle the connection wiring and an FTPSettings class to keep everything modular. This first line below makes the connection and returns the FtpWebRequest you will need to do things like download, delete, and get lists from the FTP server. A complete list and examples can be found here on the msdn.

对于任何搜索此主题的人,我只是在表示敬意。这是我最近的做法。我创建了一个 FTPProvider 类来处理连接布线和一个 FTPSettings 类来保持一切模块化。下面的第一行建立连接并返回 FtpWebRequest,您需要执行诸如从 FTP 服务器下载、删除和获取列表等操作。可以在 msdn 上找到完整的列表和示例

FtpWebRequest request = FTPProvider.GetFTPRequest(FTPProvider.Settings.FTPPath);

public class FTPProvider
{
    public FTPSettings Settings { get; set; }

    public FtpWebRequest ConnectToFtp(Uri ftpServerUri)
    {
        var request = (FtpWebRequest)WebRequest.CreateDefault(ftpServerUri);
        request.Credentials = new NetworkCredential(Settings.User, Settings.Password);
        return request;
    }

    public FtpWebRequest GetFTPRequest(string filepath)
    {
        var ftpServerUri = new Uri(filepath);
        return ConnectToFtp(ftpServerUri);
    }


}
public class FTPSettings
{
    public string FTPPath;

    public string User;

    public string Password;
}