我想要 ac# 应用程序,其中我有多个 ftp 服务器,每个服务器都有许多目录,我想检索最后修改的带有时间戳的文件。
如果我的服务器空闲或未在 ftp 服务器目录中创建文件,那么它应该通过电子邮件告诉我服务器空闲超过 30 分钟。
我可以在单个服务器地址上搜索它并获取上次修改目录的时间戳,但是我如何检查我的服务器是否空闲并在它空闲超过 30 分钟时通过电子邮件发送它并从多个服务器获取响应?
private void btnGetInfo_Click(object sender, EventArgs e) {
try {
this.Cursor=Cursors.WaitCursor;
lblStatus.Text="Working...";
txtSize.Clear();
txtTimeStamp.Clear();
Application.DoEvents();
txtSize.Text=FtpGetFileSize(txtUri.Text,
txtUsername.Text, txtPassword.Text).ToString();
txtTimeStamp.Text=FtpGetFileTimestamp(txtUri.Text,
txtUsername.Text, txtPassword.Text).ToString();
lblStatus.Text="Done";
}
catch(Exception ex) {
lblStatus.Text="Error";
MessageBox.Show(ex.Message);
}
finally {
this.Cursor=Cursors.Default;
}
}
// Use FTP to get a remote file's size.
private long FtpGetFileSize(string uri, string user_name, string password) {
// Get the object used to communicate with the server.
FtpWebRequest request=(FtpWebRequest)WebRequest.Create(uri);
request.Method=WebRequestMethods.Ftp.GetFileSize;
// Get network credentials.
request.Credentials=new NetworkCredential(user_name, password);
try {
using(FtpWebResponse response=(FtpWebResponse)request.GetResponse()) {
// Return the size.
return response.ContentLength;
}
}
catch(Exception ex) {
// If the file doesn't exist, return -1.
// Otherwise rethrow the error.
if(ex.Message.Contains("File unavailable"))
return -1;
throw;
}
}
// Use FTP to get a remote file's timestamp.
private DateTime FtpGetFileTimestamp(string uri, string user_name, string password) {
// Get the object used to communicate with the server.
FtpWebRequest request=(FtpWebRequest)WebRequest.Create(uri);
request.Method=WebRequestMethods.Ftp.GetDateTimestamp;
// Get network credentials.
request.Credentials=new NetworkCredential(user_name, password);
try {
using(FtpWebResponse response=(FtpWebResponse)request.GetResponse()) {
// Return the size.
return response.LastModified;
}
}
catch(Exception ex) {
// If the file doesn't exist, return Jan 1, 3000.
// Otherwise rethrow the error.
if(ex.Message.Contains("File unavailable"))
return new DateTime(3000, 1, 1);
throw;
}
}
public static bool GetDateTimestampOnServer(Uri serverUri) {
// The serverUri should start with the ftp:// scheme.
if(serverUri.Scheme!=Uri.UriSchemeFtp) {
return false;
}
// Get the object used to communicate with the server.
FtpWebRequest request=(FtpWebRequest)WebRequest.Create(serverUri);
request.Method=WebRequestMethods.Ftp.GetDateTimestamp;
FtpWebResponse response=(FtpWebResponse)request.GetResponse();
Console.WriteLine("{0} {1}", serverUri, response.LastModified);
// The output from this method will vary depending on the
// file specified and your regional settings. It is similar to:
// ftp://contoso.com/Data.txt 4/15/2003 10:46:02 AM
return true;
}
private void txtUri_TextChanged(object sender, EventArgs e) {
}