我有一个应用程序从SQL压缩数据库获取值,将它们分配给变量,然后调用类通过FTP发送文件。数据库中的值与最多4台服务器相关。现在我正在采用这些值中的每一个,分配一个变量然后调用它们中的每一个但是必须有更好的方法来执行此操作,例如重用变量并以某种方式使用循环。我只是想不通怎么做。任何建议都非常感谢。为了缩短我的代码,我只是显示分配的静态值,而不是查询给你一个想法。
private string ftpServer1 = @"ftp://10.0.0.0";
private string ftpServer2 = @"ftp://10.0.0.1";
private string ftpServer3 = @"ftp://10.0.0.2";
private string ftpServer4 = @"ftp://10.0.0.3";
private string ftpUser1 = @"user1";
private string ftpPass1 = @"pass1";
private string ftpUser2 = @"user2";
private string ftpPass2 = @"pass2";
private string ftpUser3 = @"user3";
private string ftpPass3 = @"pass3";
private string ftpUser4 = @"user4";
private string ftpPass4 = @"pass4";
private string ftpRemoteFile = @"myfile.exe";
private string ftpLocalFile = @"C:\Files\myfile.exe";
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
progressRichTextBox.Invoke(new MethodInvoker(delegate { progressRichTextBox.Text = "Sending to: " + ftpServer1; }));
ftp ftpClient1 = new ftp(ftpServer1, ftpUser1, ftpPass1);
ftpClient.upload(worker, ftpRemoteFile, ftpLocalFile);
progressRichTextBox.Invoke(new MethodInvoker(delegate { progressRichTextBox.Text = "Sending to: " + ftpServer2; }));
ftp ftpClient2 = new ftp(ftpServer2, ftpUser2, ftpPass2);
ftpClient2.upload(worker, ftpRemoteFile, ftpLocalFile);
BackgroundWorker worker = sender as BackgroundWorker;
progressRichTextBox.Invoke(new MethodInvoker(delegate { progressRichTextBox.Text = "Sending to: " + ftpServer3; }));
ftp ftpClient3 = new ftp(ftpServer3, ftpUser3, ftpPass3);
ftpClient.upload(worker, ftpRemoteFile, ftpLocalFile);
BackgroundWorker worker = sender as BackgroundWorker;
progressRichTextBox.Invoke(new MethodInvoker(delegate { progressRichTextBox.Text = "Sending to: " + ftpServer4; }));
ftp ftpClient4 = new ftp(ftpServer4, ftpUser4, ftpPass4);
ftpClient.upload(worker, ftpRemoteFile, ftpLocalFile);
}
答案 0 :(得分:3)
首先,我将创建一个用于保存服务器参数的结构:
struct FTPServerParams // needs better name
{
public readonly string Address;
public readonly string UserName;
public readonly string Password;
public FTPServerParams(string address, string userName, string password)
{
Address = address;
UserName = userName;
Password = password;
}
}
然后我会为列表中的所有服务器添加params:
class YourClass
{
private readonly List<FTPServerParams> ftpServersParams = new List<FTPServerParams>();
public YourClass()
{
ftpServersParams.Add(new FTPServerParams(@"ftp://10.0.0.0", @"user1", @"pass1"));
ftpServersParams.Add(new FTPServerParams(@"ftp://10.0.0.1", @"user2", @"pass2"));
ftpServersParams.Add(new FTPServerParams(@"ftp://10.0.0.2", @"user3", @"pass3"));
}
...
}
然后你可以像这样使用它:
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
foreach (var serverParams in ftpServersParams)
{
progressRichTextBox.Invoke(new MethodInvoker(delegate { progressRichTextBox.Text = "Sending to: " + serverParams.Address; }));
ftp ftpClient1 = new ftp(serverParams.Address, serverParams.UserName, serverParams.Password);
ftpClient.upload(worker, ftpRemoteFile, ftpLocalFile);
}
}