0

We are trying to form image name using below method and this method is called in for loop. We have used DateTime.UtcNow to append date time to image name.

However, our issue is for all images the same date time is getting appended, even though we have tried to use seconds and milliseconds to differentiate the image name.

As the same names are getting formed, while uploading the images, the images are getting replaced and only one image is uploaded.

Below is our code which is forming the image name.

public string FormImageName(string issuetype)
        {
            string fileName = issuetype +
                              "_" +
                              DateTime.UtcNow.ToString("yyyy-MM-dd hh:mm:ss:fff tt") +
                              ".jpeg";

            return fileName;
        }

Any help on this appreacited !

XamDev
  • 3,377
  • 12
  • 58
  • 97
  • 5
    Firstly, your filename has colons in it - that's going to cause problems. But beyond that, you say you're calling it in a loop... how many times are you calling it? If you call it more than once per millisecond (or once per clock "tick" of the system clock, which may be 15ms...) then yes, you'll get the same result more than once. – Jon Skeet Jul 08 '20 at 14:36
  • Yes, this method is called for more than once, as we have limitation of image upload to 6 (min 0 image, max 6 images), so these many times the method will be called – XamDev Jul 08 '20 at 14:42
  • Well you could try just adding 3 more `f`s to the format string to increase precision. – itsme86 Jul 08 '20 at 14:47
  • Do you actually _want_ the timestamp in the name or do just want to have unique filenames (which as well may be completely random) ? – Fildor Jul 08 '20 at 14:54
  • You can check your filenames against [Path.GetInvalidFileNameChars](https://learn.microsoft.com/de-de/dotnet/api/system.io.path.getinvalidfilenamechars?view=netcore-3.1) – Fildor Jul 08 '20 at 15:20

2 Answers2

3

Why not use an index in your loop? As the method is so fast you end up with repeated date/time.

class Program
{
    static void Main(string[] args)
    {
        for (int i = 1; i <= 10; i++)
        {
            Console.WriteLine(FormImageName("bug", i));
        }

        Console.ReadKey();
    }

    static string FormImageName(string issuetype, int index)
    {
        string fileName = $"{issuetype}_{DateTime.UtcNow.ToString("yyyy-MM-dd hh:mm:ss tt", CultureInfo.InvariantCulture)}_{index}.jpeg";
      
        return fileName;
    }
}
dperez
  • 592
  • 5
  • 11
0

Try to create a specific method to do it. You can send how many pictures you have or send the list of the pictures and do a foreach to concat a "index" for each photo.

static List<string> GenerateNames(int numberOfImages, string issuetype)
        {
            List<string> names = new List<string>();
            for (int i = 0; i < numberOfImages; i++)
                names.Add($"{numberOfImages}_{issuetype}_" +
                    $"{DateTime.UtcNow.ToString("yyyy-MM-dd hh:mm:ss:fff tt")}.jpeg");

            
            return names;
        }

enter image description here