FTPWebRequestを使用して小さなFTPクライアントを構築したいのですが、そこからユーザーを見せるためにヘッダー情報を接続して取得するための助けが必要なのですが、残りのアプリケーション。FTPサーバーに接続する
1
A
答えて
2
ファイルがFTPサーバー上で使用可能である場合には、例えばチェックしたい、あなたはまた、
http://aspalliance.com/1187_Building_a_Simple_FTP_Application_Using_C_20.all
は、私はあなたにいくつかのアドバイスを与えることがあり、ここで見つけることができ.NETで簡単なFTPクライアントを構築する方法を
ニースの記事あなたはそのサイズをちょうどチェックするかもしれません。ここで
public bool IsFtpFileExists(string remoteUri, out long remFileSize)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(BuildServerUri(remoteUri));
FtpWebResponse response;
request.Method = WebRequestMethods.Ftp.GetFileSize;
request.Credentials = new NetworkCredential(Username, Password);
try
{
response = (FtpWebResponse)request.GetResponse();
remFileSize = response.ContentLength;
return true;
}
catch (WebException we)
{
response = we.Response as FtpWebResponse;
if (response != null && response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
{
remFileSize = 0;
return false;
}
throw;
}
}
0
は、ディレクトリ情報を取得するためのサンプルです:ここでは
はその責任機能です。メソッドGetDirectoryInformationはコードサンプルの一番下にあります。これは、WindowsとUnixベースのFtpをカバーする必要があります。より完全なFTP Client code sample hereもあります。
class Ftp
{
public struct FileStruct
{
public string Flags;
public string Owner;
public string Group;
public bool IsDirectory;
public DateTime CreateTime;
public string Name;
}
public enum FileListStyle
{
UnixStyle,
WindowsStyle,
Unknown
}
private List<FileStruct> GetList(string datastring)
{
List<FileStruct> myListArray = new List<FileStruct>();
string[] dataRecords = datastring.Split('\n');
FileListStyle _directoryListStyle = GuessFileListStyle(dataRecords);
foreach (string s in dataRecords)
{
if (_directoryListStyle != FileListStyle.Unknown && s != "")
{
FileStruct f = new FileStruct();
f.Name = "..";
switch (_directoryListStyle)
{
case FileListStyle.UnixStyle:
f = ParseFileStructFromUnixStyleRecord(s);
break;
case FileListStyle.WindowsStyle:
f = ParseFileStructFromWindowsStyleRecord(s);
break;
}
if (!(f.Name == "." || f.Name == ".."))
{
myListArray.Add(f);
}
}
}
return myListArray; ;
}
private FileStruct ParseFileStructFromWindowsStyleRecord(string Record)
{
///Assuming the record style as
/// 02-03-11 07:46PM <DIR> Append
FileStruct f = new FileStruct();
string processstr = Record.Trim();
string dateStr = processstr.Substring(0, 8);
processstr = (processstr.Substring(8, processstr.Length - 8)).Trim();
string timeStr = processstr.Substring(0, 7);
processstr = (processstr.Substring(7, processstr.Length - 7)).Trim();
f.CreateTime = DateTime.Parse(dateStr + " " + timeStr);
if (processstr.Substring(0, 5) == "<DIR>")
{
f.IsDirectory = true;
processstr = (processstr.Substring(5, processstr.Length - 5)).Trim();
}
else
{
string[] strs = processstr.Split(new char[] { ' ' });
processstr = strs[1].Trim();
f.IsDirectory = false;
}
f.Name = processstr; //Rest is name
return f;
}
public FileListStyle GuessFileListStyle(string[] recordList)
{
foreach (string s in recordList)
{
if (s.Length > 10 && Regex.IsMatch(s.Substring(0, 10), "(-|d)(-|r)(-|w)(-|x)(-|r)(-|w)(-|x)(-|r)(-|w)(-|x)"))
{
return FileListStyle.UnixStyle;
}
if (s.Length > 8 && Regex.IsMatch(s.Substring(0, 8), "[0-9][0-9]-[0-9][0-9]-[0-9][0-9]"))
{
return FileListStyle.WindowsStyle;
}
}
return FileListStyle.Unknown;
}
private FileStruct ParseFileStructFromUnixStyleRecord(string record)
{
///Assuming record style as
/// dr-xr-xr-x 1 owner group 0 Feb 25 2011 bussys
FileStruct f = new FileStruct();
string processstr = record.Trim();
f.Flags = processstr.Substring(0, 9);
f.IsDirectory = (f.Flags[0] == 'd');
processstr = (processstr.Substring(11)).Trim();
_cutSubstringFromStringWithTrim(ref processstr, ' ', 0); //skip one part
f.Owner = _cutSubstringFromStringWithTrim(ref processstr, ' ', 0);
f.Group = _cutSubstringFromStringWithTrim(ref processstr, ' ', 0);
_cutSubstringFromStringWithTrim(ref processstr, ' ', 0); //skip one part
DateTime createTime = DateTime.Now;
var dateString = _cutSubstringFromStringWithTrim(ref processstr, ' ', 8);
DateTime.TryParse(dateString, out createTime);
f.CreateTime = createTime;
f.Name = processstr; //Rest of the part is name
return f;
}
private string _cutSubstringFromStringWithTrim(ref string s, char c, int startIndex)
{
int pos1 = s.IndexOf(c, startIndex);
string retString = s.Substring(0, pos1);
s = (s.Substring(pos1)).Trim();
return retString;
}
public List<FileStruct> GetDirectoryInformation(string ftpUri, NetworkCredential networkCredential)
{
List<FileStruct> directoryInfo = new List<FileStruct>();
try
{
FtpWebRequest ftpClientRequest = WebRequest.Create(ftpUri) as FtpWebRequest;
ftpClientRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
ftpClientRequest.Proxy = null;
ftpClientRequest.Credentials = networkCredential;
FtpWebResponse response = ftpClientRequest.GetResponse() as FtpWebResponse;
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.ASCII);
string datastring = sr.ReadToEnd();
response.Close();
directoryInfo = GetList(datastring);
return directoryInfo;
}
catch (Exception e)
{
return directoryInfo;
}
}
}
関連する問題
- 1. InformaticaでFTPサーバーを接続する
- 2. FTPサーバーに接続し、ファイルにアクセス
- 3. .net 2.0でFTPサーバーに接続
- 4. Ionic 2 FTPサーバーへの接続/ダウンロード/アップロード
- 5. FTPサーバーの接続問題(FTP配布問題)
- 6. Powershell FTP接続
- 7. kafkaをftpに接続するには
- 8. Teamcity ftpアップロードエラー接続
- 9. Curl FTP接続プール
- 10. はPHPでFTPサーバーに接続できません、ftp_connect()
- 11. Microsoft Windows CMDでWindows FTPサーバーに接続できません
- 12. LAN外のIIS FTPサーバーに接続できません
- 13. Javaでローカルホストに接続するFTP
- 14. PHPStormのFTP接続に失敗する
- 15. 他のftpサーバに接続する.db
- 16. STORコマンドがFTPサーバーとの接続を破棄します
- 17. アンドロイドでFTPサーバーに接続すると問題が発生しますか?
- 18. Zehon FTP接続を閉じるには
- 19. ローカルのftpサーバーとAndroidを接続してください
- 20. JavaでTLS/SSL(FTPS)サーバー経由でFTPに接続する方法
- 21. ファイルをダウンロードした後にFTPサーバー接続を切断する方法は?
- 22. VB.NET MySQLとFTP接続
- 23. ソナタメディアバンドルftp接続の設定?
- 24. Magentoの接続FTP設定
- 25. Android FTPに接続するには?私は、ApacheのFTPを介してサーバーに接続しようとしています
- 26. FTP接続に失敗しました
- 27. ftp経由でwampに接続
- 28. C#でCerberus FTP APIに接続
- 29. ftpに接続できません - バッチファイル
FTPはHTTPとは異なり、「ヘッダ」という概念を持っていません。コマンド応答ファイル転送プロトコルです。 FTPWebRequestを使用するサンプルを探していますか? –
申し訳ありませんが、私はそれが正しい用語だと思う "バナー"情報を意味しています。サンプルコードはかなりクールでしょう! – user377419