以递归方式查找并列出具有子目录和时间的目录中的最新修改文件?

时间:2013-06-25 09:53:39

标签: c# ftp

我想要一个c#应用程序,其中我有多个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) {
}

0 个答案:

没有答案
相关问题