我想建立一個FTPWebRequest的小型FTP客戶端,我只想幫助連接和抓取頭信息以顯示用戶從那裏我想我可以找出其餘的應用程序。C# - 連接到FTP服務器
1
A
回答
2
好文章如何構建在.NET中一個簡單的FTP客戶端你會發現這裏http://aspalliance.com/1187_Building_a_Simple_FTP_Application_Using_C_20.all
此外,我可以給你一些建議,比如你要檢查,如果文件可以在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. 連接到ftp服務器
- 2. 使用ftp連接到服務器
- 3. PHP連接到FTP服務器
- 4. 無法連接到FTP服務器
- 5. Windows FTP連接說無法連接到遠程服務器
- 6. 連接到FTP服務器時保持會話C#
- 7. FTP干擾NAT服務器連接
- 8. 使用JAVA連接到遠程服務器的FTP連接
- 9. 連接超時,但連接到FTP服務器
- 10. ftp ftp服務器C
- 11. InternetConnect無法通過FTP代理連接到FTP服務器
- 12. 無法連接到FTP服務器android ftp應用程序
- 13. 連接到FTP服務器並從FTP下載文件到本地機器
- 14. 如何在C#中使用SSH FTP或SFTP連接到FTP服務器?
- 15. FTP服務器連接模擬
- 16. 正確檢查FTP服務器連接
- 17. 我無法連接FTP服務器
- 18. 如何連接FTP服務器?
- 19. PowerShell連接到FTP服務器並獲取文件
- 20. FTP上傳 - 無法連接到遠程服務器
- 21. 未連接到服務器
- 22. C#Exchange服務器連接
- 23. 服務器到服務器的連接
- 24. 連接到C#服務器從JavaScript
- 25. 從iOS連接到C#服務器
- 26. 連接到smtp服務器窗口C++
- 27. c#未能連接到websocket服務器
- 28. 連接到C#中的WAN服務器#
- 29. 連接C#到SQL服務器
- 30. C#連接到mysql服務器
與HTTP不同,FTP沒有「標題」概念 - 它是一種命令響應文件傳輸協議。你在尋找一個使用FTPWebRequest的例子嗎? – 2011-01-09 13:15:22