在c#循环中重用变量

本文关键字:变量 循环 | 更新日期: 2023-09-27 18:05:23

我有一个应用程序,从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);
    }

在c#循环中重用变量

首先,创建一个结构来保存服务器参数:

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;
    }
}

然后我将所有服务器的参数放在一个列表中:

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);
    }
}