Home > Blockchain >  Why getting exception when trying to create a folder name based on date time?
Why getting exception when trying to create a folder name based on date time?

Time:10-27

In the screenshot on the left, what do I get on the textBox Text, and on the right is the exception when clicking on the continue button.

Exception

The code of the method to create the folder/s :

private void CreateDownloadFolders()
        {
            string locationToCreateFolderRadar = textBoxRadarPath.Text;
            string locationToCreateFolderSatellite = textBoxSatellitePath.Text;
            string folderName = "";
            string date = DateTime.Now.ToString("ddd MM.dd.yyyy");
            string time = DateTime.Now.ToString("HH.mm tt");
            string format = "{0} on {1} At {2}";
            folderName = string.Format(format, date, time);
            Directory.CreateDirectory(locationToCreateFolderRadar   folderName);
            Directory.CreateDirectory(locationToCreateFolderSatellite   folderName);
        }

The button click event that calls the CreateDownloadFolders method :

private async void btnStart_Click(object sender, EventArgs e)
        {
            CreateDownloadFolders();

            urls = new List<string>();

            lblStatus.Text = "Downloading...";

            rad.GetRadarImages();
            await Task.Delay(5000);
            foreach (string link in rad.links)
            {
                urls.Add(link);
            }

            await sat.DownloadSatelliteAsync();
            foreach (string link in sat.SatelliteUrls())
            {
                urls.Add(link);
            }

            urlsCounter = urls.Count;

            await DownloadAsync();
        }

Maybe because the button click event is type async?

CodePudding user response:

your issue is on this line:

  folderName = string.Format(format, date, time);

you are sending only two parameters and the format is expecting 3:

 string format = "{0} on {1} At {2}";

you have two ways to solve this:

1:

 string format = "{0} on {1} At X"; // expect only 2 parameters

or

2:

   folderName = string.Format(format, date, time, "something else"); // send all 3 parameters
  • Related