简体   繁体   中英

How can I make the file names to be raise by one each time a file is downloading?

This is the code:

for (int i = 0; i < dateTime.Count; i++)
{
    string result = dateTime[i].ToString("yyyyMMddHHmm");
    link = "http://www.sattest.com + "&time=" + result + "&ir=" +
                       infraredorvisual;
    string filePath = Path.Combine(satimagesdir, "SatImage" + i + ".GIF");
    try
    {
        client1.DownloadFile(link, filePath);
    }
    catch (Exception e)
    {
        DannyGeneral.Logger.Write(e.ToString());
    }
}

In the first time when the program is running it's downloading 9 files. In dateTime List there are 9 items to download each time. The timer I'm using will download the files every 10 minutes.

The first I have 9 files on the hard disk. Then after 10 minutes I want that the next downloaded files will be named as SatImage10 then SatImage11...SatImage18

Next 10 minutes on the hard disk I want to see SatImage19 then SatImage20...SatImage27

How can I do it? The problem is that dateTime will contain always 9 files so the variable I will name the files all the time from SatImage0 to SatImage8.

Use a variable to mark where you left off:

private int last;

And increment it by the number of items each iteration, and use it to set the image name:

for (int i = 0; i < dateTime.Count; i++)
{
    ...
    string filePath = Path.Combine(satimagesdir, "SatImage" + (i + last) + ".GIF");
    ...
}
last += dateTime.Count;

If you close the application between downloads, you will need to iterate the files in the directory and extract the latest number from the file name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM